Click here to Skip to main content
15,039,359 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am able to typecast reference of vector<void*> to reference of vector<Foo*> whereas I am unable to typecast vector<void*> to vector<Foo*>. I am getting the error C2440: 'reinterpret_cast' : cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty>' why?

And I am able to typecast void* to Foo* without getting compiler error.
C++
void* g =&foo;
reportFooVector2(reinterpret_cast<Foo*>(g));


Below is my entire code.

#include "stdafx.h"
struct Foo
{
  string s;
  int i;
};


void reportFooVector( vector <Foo*> * pvf )
{
 
}


void reportFooVector1( vector <Foo*> pvf )
{
}

void reportFooVector2( Foo *pvf )
{
}


int main()
{
	struct Foo foo = {"foo",  5};
	struct Foo goo = {"goo", 10};
	void* g =&foo;
	reportFooVector2(reinterpret_cast<Foo*>(g));
	vector <void *> vf;
	vf.push_back(&foo);
	vf.push_back(&goo);
	reportFooVector1( reinterpret_cast< vector < Foo * >  >(vf));
	reportFooVector( reinterpret_cast< vector < Foo * > * >(&vf));
}

In the above program I am getting the compiler error C2440: 'reinterpret_cast' : cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty>' when calling the line reportFooVector1( reinterpret_cast< vector < Foo * > >(vf));

Could you please anyone tell the reason for it?

What I have tried:

I browsed the net but could not get any explanation.
Posted
Updated 26-Sep-18 7:25am
v3

The reason is simple: reinterpret_cast only works with pointers (this is the short answer, for the detailed one, feel free to read the documentation: reinterpret_cast conversion - cppreference.com[^]).
   
v2
Comments
CPallini 25-Sep-18 8:59am
   
Exactly.
Type casting can lead to strange errors when done wrong, so it is better to avoid this or handle it with greatest care.

So avoid type casting to void* because the programmer and the compiler are loosing precious informations about the objects. Any up cast is leading to different handling of functions and has so the danger of a crash.

Normally it is a sign of poor design or a coding bug. Maybe NOT in your example, but your real code.
   
One thing I find helpful in these situations is to use type definitions. Here are a couple types I would define in this situation :
C++
typedef struct Foo * pFoo;
typedef std::vector<pFoo>  vecpfoo;
Then I would adjust your functions to this :
C++
void reportFoo( int index, pFoo pf )
{
   cin << "Foo item: " << index << endl;
   cin << "Member s: " << pf->s << endl;
   cin << "Member i: " << pf->i << endl;
}

void reportFooVector( vecpfoo & vpf )
{
   // this should be written with an iterator loop
   int count = (int)vpf.size();
   for( int i; i < count; ++i )
       reportFoo( i+1, vpf[i] );
}
and I would rewrite the main function like this:
C++
int main()
{
   struct Foo foo = { "foo", 5 };
   struct Foo goo = { "goo", 10 };
   vecpfoo vf;
   vf.push_back( &foo );
   vf.push_back( &goo );

   reportFooVector( vf );
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900