关于模板参数的SFINAE +嵌套类的外部定义 [英] SFINAE on template parameter + outside definition of nested class

查看:122
本文介绍了关于模板参数的SFINAE +嵌套类的外部定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个类A,用类型T模板化和一个enable_if子句,它包含一个嵌套类I(它是A的用户定义迭代器) ,像这样:

模板< typename T,std :: enable_if_t< std :: is_arithmetic< T> :: value>> 
A级{
public:
class I;
};





我找不到在A之外定义我的正确方法,我理解错误信息(看看你有什么尝试?会议​​)我得到但我想念的是哪种方法可以做我想做的事。



我尝试过:



我试过这个:

模板< ; typename T,std :: enable_if_t< std :: is_arithmetic< T> :: value>> 
A类< T> ::我{

};



但是它给出了错误

Quote:

无法将默认模板参数添加到类模板成员的定义中





这个:

模板< typename T> 
A类< T> ::我{

};



给出了这个错误:

引用:
在模板太少模板参数重新声明

解决方案
我自己回答,我解决了这个问题,将enable_if检查从模板参数列表移动到构造函数,一切正常,如下所示:

 template< typename T> 
类A {
公共:
A(标准:: initializer_list<类型名的std :: enable_if<的std :: is_arithmetic< T> ::值,T> ::类型→1){ }
class I;
};

模板< typename T>
A类< T> :: I {
public:
int prova(){return 1; };
};


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

解决方案

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; };
};


这篇关于关于模板参数的SFINAE +嵌套类的外部定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆