Is it possible to call a constructor with template arguments if the class is also a template?
#include <stdio.h> #include <iostream> template <class A> struct Class { template <class B> Class(B arg) { std::cout << arg << std::endl; } }; int main() { Class<int> c<float>(1.0f); Class<int>* ptr = new Class<int><float>(2.0f); return 0; }
edit: so I assume that the only way to invoke a specific template constructor is to invoke it using casted paramterers on the desired template type:
#include <stdio.h> #include <iostream> template <class A> struct Class { template <class B> Class(B arg) { std::cout << arg << std::endl; } Class(double arg) { std::cout << "double" << std::endl; } Class(float arg) { std::cout << "float" << std::endl; } }; int main() { Class<int> c(1.0f); Class<int>* ptr = new Class<int>((double)2.0f); return 0; }
// these outputs: double float
edit2: but what happens to the design arguments of the template that are not part of the constructor arguments themselves?
template <class B, class C> Class(B arg) { }
source share