Template class specialization dependent on constructor arguments?
I am investigating a problem with C++ class templates. One class template is specialized but the compiler does not always choose to use the specialization. I found that the constructor arguments seem to influence this:
temlate <class T>
class MyClass { /*some constructors*/ };
template<>
class MyClass <int>
{ void foo(); /*some constructors*/}
MyClass<int> test1;
test1.foo(); //works
MyClass<int> test1("hallo");
test1.foo(); //doesn't work (foo does not exist, compiler uses unspecialized version.)
I haven't managed to create a sample that shows the problem because the constructor arguments are pretty complex (and the problem does not occur with simple arguments).
But my question is simply this: Is it possible, that constructor arguments influence the choice of the compiler? How?
I am working with Visual C++ 2008.
Thanks a lot!
---- EDIT:
It seems like we have identified the problem: If the template specialization was not part of all the translation units in the static library that we build, the problem occurs. But it disappears, if there are no other translation units.
I found http://codeidol.com/cpp/cpp-templates/Instantiation/Implementati开发者_如何学Goon-Schemes/ and it seems to me that with the Greedy Implementation the phenomena we observed can be explained.
Does anybody know which implementation schemes are actually used by MSVC and GCC?
But my question is simply this: Is it possible, that constructor arguments influence the choice of the compiler? How?
No, because you are telling it which type you want to use :
MyClass<int> test1;
test1.foo(); //works
is always creating objects of the specialized type.
A global template function would be a type, and compiler would use the function arguments for type deduction. Similarly, the "type" arguments for class template would be used as template arguments for class.
But you want a constructor (which is part of some type), to be participated in the template-type deduction - which is not possible.
精彩评论