Reputation: 7994
it's possible to have access to template "type", for instance in the std
std::vector<int>::size_type
is it possible to have the same thing for objects passed as template parameters? For instance:
template<int i>
class A {
//?
};
A<3> instance;
int number = instance::???? //<--- assigns 3 to number
is it possible to get the 3 passed in the object type again at runtime? Without creating a specific member in the A class (which would increase the size of the object)
thanks
Upvotes: 3
Views: 430
Reputation: 97938
template<int i>
class A {
public:
enum { number = i };
};
int main() {
A<3> instance;
std::cout << instance.number;
return 0;
}
Upvotes: 2
Reputation: 308121
The type of the variable is known to the compiler at compile time, it's just a matter of getting it to give it up.
template<int i>
int get(const A<i> & instance)
{
return i;
}
Upvotes: 5