[英]C++ void_t SFINAE false_type true_type can't get specialization
It all seemed simple.这一切似乎都很简单。
Shouldn't has_member_type::value be true for T=vector in my code below?在下面的代码中,对于 T=vector,has_member_type::value 不应该为真吗? Sorry if it had already be answered.
对不起,如果它已经被回答。 I looked for a while, but couldn't find an answer.
我找了一会儿,但找不到答案。
#include <iostream>
#include <type_traits>
// has_value_type
template <typename, typename = std::void_t<>>
struct has_value_type: std::false_type {};
template <typename T>
struct has_value_type<T, std::void_t<typename T::value_type>>: std::true_type {};
int main()
{
std::cout << "bool: " << std::boolalpha << has_value_type<bool>::value << std::endl;
std::cout << "vector_has: " << std::boolalpha << has_value_type<std::vector<int>>::value << std::endl;
std::cout << "true_type: " << std::boolalpha << std::true_type::value << std::endl;
std::cout << "false_type: " << std::boolalpha << std::false_type::value << std::endl;
return 0;
}
Output: Output:
bool: false
vector_has: false
true_type: true
false_type: false
I'm using clang++我正在使用clang++
clang version 9.0.0 (tags/RELEASE_900/final)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
If you use the libc++
standard library implementation, and don't include <vector>
, then a forward declaration of std::vector
is introduced with:如果您使用
libc++
标准库实现,并且不包含<vector>
,则引入std::vector
的前向声明:
#include <iostream> // forward declares std::vector
This forward declaration allows the code to compile, but the sfinae check returns false
since there are no member types defined for std::vector
yet.此前向声明允许代码编译,但 sfinae 检查返回
false
,因为尚未为std::vector
定义成员类型。
You can't rely on this, and should always #include<vector>
if you want to use std::vector
in any context.你不能依赖这个,如果你想在任何上下文中使用
std::vector
,应该总是#include<vector>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.