[英]Kindly explain problem in following function template declaration (Underlying cause)
In following code, could anyone explain why do I get following error: "error: no type named 'type' in struct std::enable_if<false, double>
在下面的代码中,谁能解释为什么我会收到以下错误:“错误:
struct std::enable_if<false, double>
中没有名为‘type’的类型
According to my understanding of enable_if_t, there should not be any problem at compile time if I am not using function p.根据我对enable_if_t的理解,如果我不使用function p,编译时应该没有问题。 It should simply not get generated for simple types.
它不应该为简单类型生成。
But when I change condition to !is_class_v<T>
, it works fine for simple types but then it stops working for class types.但是当我将条件更改为
!is_class_v<T>
时,它适用于简单类型但随后它停止适用于 class 类型。
template<typename T>
class Smart_class
{
public:
enable_if_t<is_class_v<T>, T> p(T t)
{
};
};
void f()
{
Smart_class<double> a;
}
Quoting from temp.inst/3.1 :引用自temp.inst/3.1 :
The implicit instantiation of a class template specialization causes the implicit instantiation of the declarations , but not of the definitions, of the non-deleted class member functions ...
class 模板特化的隐式实例化会导致未删除的 class 成员函数的声明的隐式实例化,而不是定义的隐式实例化......
This is your case, the declaration of p
member function is instantiated and it refers to the non-existing return type.这是你的情况,
p
成员 function 的声明被实例化,它引用不存在的返回类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.