Click here to Skip to main content
15,877,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I have a class A, templated with a type T and an enable_if clause, which contains a nested class I (which is meant to be a user-defined iterator for A), like this:
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value>>
class A {
   public:
      class I;
};


I can't find the proper way to defined I outside of A, I understand the error messages (see "What have you tried?" session) I get but what I miss is which is the correct way to do what I want..

What I have tried:

I tried this:
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value>>
class A<T>::I {

};

but it gives the error
Quote:
cannot add a default template argument to the definition of a member of a class template


while this:
template <typename T>
class A<T>::I {

};

gives this error:
Quote:
too few template parameters in template redeclaration
Posted
Updated 27-Nov-18 22:19pm
Comments
CPallini 27-Nov-18 8:53am    
I guess I should be in template argument list.

1 solution

I answer myself, I solved this moving the enable_if check from the template parameters list to the constructor and everything works fine, like this:
template <typename T>
class A {
   public:
      A(std::initializer_list<typename std::enable_if<std::is_arithmetic<T>::value,T>::type> l) {}
      class I;
};

template <typename T>
class A<T>::I {
   public:
      int prova() { return 1; };
};
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900