Reputation: 193
I am creating a trait with a template, which has as template parameter a trait instance. The code is more complicated, but for this question I keep the code simple. The code is the following:
#include <iostream>
template<int i>
struct A
{
static const int value = i;
};
template<typename a>
struct B
{
static const int value = a::value;
};
int main(int argc, char *argv[]) {
std::cout << A<3>::value << std::endl; // 3
std::cout << B< A<3> >::value << std::endl; // 3
return 0;
}
This works, but now I want to change typename
to something like A<int i>
to make sure B
can only be called when you pass an instance of A<int i>
as template parameter.
If I do this I get the following error:
test.cpp:11:17: error: template argument 1 is invalid template<\A<\int i> a> ^ test.cpp:14:30: error: 'a' has not been declared static const int value = a::value; ^
How do I do this?
Upvotes: 1
Views: 122
Reputation: 66210
How do I do this?
Using specialization
template <typename>
struct B;
template <int I>
struct B<A<I>>
{ static const int value = A<I>::value; }; // or also value = I;
Upvotes: 1