|
|
Hi,
I want to fill a vector that memorize multiple instances of a template class (those instances potentially use different template type).
Here my question in code :
template< class T >
class CTest
{
}
CTest< int > a;
CTest< int > b;
CTest< double > c;
vector< CTest< int > * > myints;
myints.push_back( &a );
myints.push_back( &b );
vector< CTest * > all;
all.push_back( &a );
all.push_back( &c );
In short, I don't find the correct way to fill my vector 'all'...
All my tries give a "cannot convert parameter from something to another thing" error.
Anyone know if it is possible and how ?
Thanks in advance for all your answers,
SV
|
|
|
|
|
forwardsim wrote: All my tries give a "cannot convert parameter from something to another thing" error.
Why post an interpretation of the message you see rather than the real message, which would actually help people to figure out what is wrong.
It's time for a new signature.
|
|
|
|
|
Thanks for your interest.
I didn't write the real message for three reasons. First, I try a lot of things and I got a lot of different messages, but all construct from this general sentence. Second, when I copied an example of error, a lot of characters didn't copied correctly. Finally, I was expecting anyway that interesting people will try it.
If you try the example code in my post, the error shown is :
error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'CTest<t> *__w64 ' to 'CTest *const &'
Hope that helps you figure out what to do...
Thanks again,
SV.
|
|
|
|
|
Quick C++ quiz for you...
If I define a template class:
template<typename T>
class test
{
};
What's the type of a and b in the following declarations?
test<A> a;
test<B> b;
Quick hint - they're not the same.
Now as std::vectors can only hold objects of a single type it's impossible to mix objects of different instantiations of a template class.
Incidentally are you sure you wrote:
std::vector< test * > all;
or something similar? To the best of my knowledge that shouldn't compile and on a bunch of compilers I tried it on they all complained mightily about the fact that test was a template.
Cheers,
Ash
|
|
|
|
|
Thanks.
I understand your point since it's my feeling.
The fact that :
std::vector< test * > all;
passes without complains from my compiler gives me some hopes. Unfortunately, I don't have any other compilator around to try it.
I'm using Microsoft Visual C++ 2005.
SV.
|
|
|
|
|
Ouch, looks like a nasty compiler bug - it should reject the declaration of the vector.
Just had a quick prod with VC++ 2005 - it allows the declaration/definition of the object but you can't do a lot with it, thankfully.
Cheers,
Ash
|
|
|
|
|
Hi,
I tried Microsoft Visual Studio 2010 and
std::vector< test * > all;
doesn't compile.
All my hopes disappeared...
Thanks again...
SV.
|
|
|
|
|
In addition to the post from Ash, you could get to it like this
class base {};
template< class T >
class CTest : public base
{
}
std::vector<base*> all;
and make base the interface to access what you need in the template classes.
|
|
|
|
|
Hi,
It was my first work around test. Unfortunately, it didn't work because I wasn't able to
down-cast from the parent class to the child class. I guess the problem arise only when a member
function has the template class in argument...
Here what happens.
class base
{
};
template< class T >
class CTest : public base
{
public:
T mem;
void add( CTest< T > i )
{
mem += i.mem;
}
template < class S >
void add( CTest< S > i )
{
}
};
std::vector<base*> all;
CTest< int > a;
CTest< double > b;
all.push( (base*) &a );
all.push( (base*) &b );
all[0]->add( all[1] );
I hope my example is clear enough...
SV.
modified on Wednesday, August 18, 2010 5:03 PM
|
|
|
|
|
I probably shouldn't encourage this - heterogeneous collections usually means there's something a bit screwy in your design. But where OO and generic programming collide you can use runtime type information:
class test
{
public:
virtual test &operator+=( const test &other ) = 0;
};
template <typename T>
class T_test : public test
{
public:
T_test( T initial ) : value_( initial )
{
}
virtual test &operator+=( const test &other )
{
if( const T_test<T> *p = dynamic_cast<const T_test<T> *>( &other ) )
{
value_ += p->value_;
}
return *this;
}
private:
T value_;
};
Then you can use it something like:
int main()
try
{
T_test<int> a( 100 );
T_test<double> b( 100.0 );
T_test<int> c( 200 );
test *ptrs[] = { &a, &b, &c };
std::vector<test *> all( ptrs, ptrs + 3 );
T_test<int> result( 0 );
for( std::vector<test *>::const_iterator iter = all.begin(); iter != all.end(); ++iter )
{
result += **iter;
}
}
catch( const std::exception &e )
{
std::cout << e.what() << std::endl;
}
catch( ... )
{
std::cout << "Something went wrong, no idea what!" << std::endl;
}
If this does the sort of thing you're after please consider how to get rid of the dynamic_cast. Oh, and if anyone asks who told you to do this please don't mention my name, tell them it's some other random hacker.
Cheers,
Ash
Edited for tabs and adding the catch(...)
|
|
|
|
|
Hi Ash,
I did think to use a dynamic cast, but I didn't manage to quickly find how to structure things to make it works. Your example is great, simple and fully compatible with my code. I found another solution by restructuring my code, but this solution is not so great even if I don't have to rely on dynamic casting.
I'll think about it tomorrow and choose what seems the best solution. Quickly, I think an approach base on your example will be more interesting, but a little slower because of the dynamic casting. Shouldn't be a problem since this is not a critical part of my code.
Thanks a lot...
Steve.
|
|
|
|
|
Dynamic_cast isn't that slow - it's of the same sort of order as a virtual function dispatch which is about the same order as calling a function in a shared library. The one big bugger with it is that the compiler has problems inlining the operation as it's got no idea what's being thrown at it.
10:1 your code's got far grosser inefficiencies in it than a dynamic_cast. Whenever I profile my apps I'm always surprised at where my code spends it's time. Anyway, good luck!
Cheers,
Ash
|
|
|
|
|
You are running on a complex thing (for C++) generically named as "mumtimethod".
C++ function dispatching works with v-tables only for the one and only implicit parameter of a virtual member function (this ).
If you need to dispatch based on two parameters (that is: when not only the object type but also a function parameter is important) you need to go across two indirections. And since V-tables holds only one you have to workaround.
There are various articles about "multimethods" (try google with that key) with different approaches, but they always have to fall into two indirections, two switches, one swith and one indirection etc.
The complexity of the coding process will be in any case N2 (you have to supply implementation for all the combination) and the execution complexity (whatever you use V-function of switches) will always be "two indrections".
My invite is to don't drop solution only becasue they use virtual function or because they use dynamic_cast: the fact that such implemetations are always slower is a mith: they are slower than direct calls, but where indirections are needed, they are fast exactly like whatever other indirection mechanism. If you define a vector of function pointer to dispatch the calls ... like many of those article, with more or less smart or complex way do, you are implementing in source a v-table!
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Thanks for the information.
I'm going to read about that...
SV.
|
|
|
|
|
Adding some food for thought
#include <vector>
class base
{
public:
template<class T>
static void sum(T &dest, const T &src)
{
dest.privateadd(src);
}
template<class T, class U>
static void sum(T &dest, const U &src)
{
}
private:
template<class T>
void privateadd(const T &t)
{
virtualadd(&t);
}
virtual void virtualadd(const base *arg) = 0;
};
template< class T >
class CTest : public base
{
public:
T mem;
private:
virtual void virtualadd(const base *arg)
{
const CTest<T> *other = static_cast<const CTest<T>*>( arg );
mem += other->mem;
}
};
int main()
{
std::vector<base*> all;
CTest<int> a;
CTest<double> b;
base::sum(a, b); base::sum(a, a); base::sum(a, 1); all.push_back(&a); all.push_back(&b); base::sum(*all[0], *all[1]);
base::sum(*all[0], *all[0]);
}
|
|
|
|
|
Pretty cool!
I was wondering how I could get rid of the dynamic_cast but completely missed the idea of using free functions (or statics) to do it.
Cheers,
Ash
|
|
|
|
|
Still, there must be a better way to attack the problem in the first place. The base class get bloated rather quickly when extending it.
I was a bit annoyed though that I couldn't get back to the a.add(b) syntax. One could of course add that method to the CTest<T> class and use it when you don't have to rely on pointers to the base class. But it would have been nice to always be able to use it.
|
|
|
|
|
Thanks everyone for your great comments!!!!!
I just wondering about something based on this code. In fact, I thought of something pretty similar but I didn't even try it since I was thinking that shouldn't work.
When the calls are made :
base::sum(*all[0], *all[1]);
base::sum(*all[0], *all[0]);
I was just thinking that *all[0] and *all[1] will be both of type 'base' than the dest and source of the static function sum will always be the same. Always ending in this function
template< class T >
static void sum(T &dest, const T &src)
{
dest.privateadd(src);
}
and never in this one
template<class T, class U>
static void sum(T &dest, const U &src)
{
}
Ultimately we will enter the virtualadd of class CTest with different type to handle. Then, the static_cast should fail...
Since you both think it will work, I'm just wondering where I'm wrong...
SV.
|
|
|
|
|
Bum, good spot! Hang on, why are you the one asking us questions?
Back to the drawing board...
Ash
|
|
|
|
|
Oh, crap. (I was tempted to say that this new problem could easily be solved with a dynamic_cast, but I will refrain from that )
I will have another look.
|
|
|
|
|
something like this would also work, ít depends on what you are up to...
class base {
public:
virtual void accept(class visitor&) = 0;
};
template<typename T>
class impl : public base {
public:
virtual void accept(class visitor& v);
};
class visitor {
public:
void operator()(base* b) {
b->accept(*this);
}
void visit(impl<int>& pimpl) {
std::cout << "i am an int" << std::endl;
}
void visit(impl<double>& pimpl) {
}
};
template<typename T>
void impl<T>::accept(visitor& v) {
v.visit(*this);
}
void foo() {
std::vector<base*> vec;
vec.push_back(new impl<int>);
vec.push_back(new impl<double>);
visitor v;
std::for_each(vec.begin(), vec.end(), v);
}
modified on Monday, August 30, 2010 1:55 AM
|
|
|
|
|
I was wondering where your post went This looks a lot neater.
The only drawback I can see is that you have to foresee all possible values of T. As you say, it depends on what needs to be solved.
|
|
|
|
|
Yeah, sorry, I am writing this down on my PocketPC so it's bit hard to see what I actually wrote. I removed the old post since I couldn't edit it anymore.
To foresee all values of T is the price for having the automatic dispatching wo. rtti. You could go the acyclic visitor way with one dyna-cast in accept...
|
|
|
|
|
hello guys...can we overload the main()?? It is just a question and i've no intentions to do so??
MY ANSWER:
May be we could do it(i dont know how), but since the main() is the driver of our program so it should be punishable if someone does it
|
|
|
|