This question seems very closely related to the following, yet I do not fully understand it yet.
- Can the template parameters of a constructor be explicitly specified?
- Template constructor in a class template - how to explicitly specify template argument for the 2nd parameter?
I want to have a template class that does something non-trivial in the constructor, depending on another template type. A minimal example would be this here:
template <typename A>
class Class {
public:
template <typename B>
Class();
private:
int i;
};
template <typename A, typename B>
Class<A>::Class() {
B b;
i = b.get_number();
}
That does not compile with GCC:
$ env LC_ALL=C g++ --std=c++11 -c template.cpp
template.cpp:14:1: error: prototype for 'Class<A>::Class()' does not match any in class 'Class<A>'
Class<A>::Class() {
^~~~~~~~
template.cpp:7:5: error: candidate is: template<class A> template<class B> Class<A>::Class()
Class();
^~~~~
Compiling with Clang gives other errors:
$ env LC_ALL=C clang++ --std=c++11 -c template.cpp
template.cpp:13:1: error: too many template parameters in template redeclaration
template <typename A, typename B>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template.cpp:3:1: note: previous template declaration is here
template <typename A>
^~~~~~~~~~~~~~~~~~~~~
1 error generated.
I can get it to compile when I attach the template parameter B
to the whole class.
template <typename A, typename B>
class Class {
public:
Class();
private:
int i;
};
template <typename A, typename B>
Class<A, B>::Class() {
B b;
i = b.get_number();
}
Is there any way to contain the influence of template parameter B
to the constructor? Or would I have to attach it to the whole class?