[英]C++ SFINAE void_t not working
I tried running following code, which should rely on void_t
trick, where more specialized class template should be chosen (in this case second one) 我尝试运行以下代码,该代码应依赖
void_t
技巧,应在其中选择更专业的类模板(在本例中为第二个模板)
#include <iostream>
#include <type_traits>
template <class ...>
using void_t = void;
template <class T, class = void>
struct is_incrementable : public std::false_type { };
template <class T>
struct is_incrementable<T, void_t<decltype(++(std::declval<T>()))>> : public std::true_type { };
int main()
{
std::cout << std::boolalpha;
std::cout << is_incrementable<int>::value << std::endl;
return 0;
}
I am using MSVC 2015. However, the result of is_incrementable<int>::value
is false. 我正在使用
is_incrementable<int>::value
。但是, is_incrementable<int>::value
为false。 Is there anything wrong with my code or is there problem with my compiler? 我的代码有什么问题还是我的编译器有问题?
std::declval<T>
has return type T&&
, so std::declval<int>()
is an rvalue of type int
. std::declval<T>
具有返回类型T&&
,因此std::declval<int>()
是int
类型的右值 。 The result of "false" is telling you that an int
rvalue is not incrementable, which is correct. 结果“ false”告诉您
int
rvalue不可递增,这是正确的。
You can replace std::declval<T>
with std::declval<T&>
to get the program to tell you whether an lvalue of type T
is incrementable. 您可以将
std::declval<T>
替换为std::declval<T&>
以使程序知道T
类型的左值是否可递增。 If you make this change to your program, it should print "true". 如果对程序进行了更改,则应打印“ true”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.