C++ 如果使用std::is_引用编译失败,则启用_
像C++ 如果使用std::is_引用编译失败,则启用_,c++,c++11,typetraits,enable-if,C++,C++11,Typetraits,Enable If,像std::reference\u wrapper在封面下使用一个指针来存储一个“reference”,我试图用下面的代码做一些类似的事情 #include <type_traits> struct Foo { void* _ptr; template<typename T> Foo(T val, typename std::enable_if < std::is_r
std::reference\u wrapper
在封面下使用一个指针来存储一个“reference”,我试图用下面的代码做一些类似的事情
#include <type_traits>
struct Foo
{
void* _ptr;
template<typename T>
Foo(T val,
typename std::enable_if
<
std::is_reference<T>::value,
void
>::type* = nullptr)
: _ptr(&val)
{ }
};
int main()
{
int i = 0;
int& l = i;
Foo u2(l);
return 0;
}
#包括
结构Foo
{
无效*\u ptr;
模板
富(T瓦尔),
typename std::启用\u如果
<
std::is_reference::value,
无效的
>::type*=nullptr)
:_ptr(&val)
{ }
};
int main()
{
int i=0;
int&l=i;
富u2(l);
返回0;
}
但是,这无法编译:
CXX main.cpp
main.cpp: In function ‘int main()’:
main.cpp:23:13: error: no matching function for call to ‘Foo::Foo(int&)’
main.cpp:23:13: note: candidates are:
main.cpp:8:5: note: template<class T> Foo::Foo(T, typename std::enable_if<std::is_reference<_Tp>::value, void>::type*)
main.cpp:8:5: note: template argument deduction/substitution failed:
main.cpp: In substitution of ‘template<class T> Foo::Foo(T, typename std::enable_if<std::is_reference<_Tp>::value, void>::type*) [with T = int]’:
main.cpp:23:13: required from here
main.cpp:8:5: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
main.cpp:3:8: note: constexpr Foo::Foo(const Foo&)
main.cpp:3:8: note: no known conversion for argument 1 from ‘int’ to ‘const Foo&’
main.cpp:3:8: note: constexpr Foo::Foo(Foo&&)
main.cpp:3:8: note: no known conversion for argument 1 from ‘int’ to ‘Foo&&’
CXX main.cpp
main.cpp:在函数“int main()”中:
main.cpp:23:13:错误:调用'Foo::Foo(int&')没有匹配的函数
main.cpp:23:13:注:候选人为:
main.cpp:8:5:注意:模板Foo::Foo(T,typename std::enable_if::type*)
main.cpp:8:5:注意:模板参数扣除/替换失败:
main.cpp:替换“模板Foo::Foo(T,typename std::enable_if::type*)[带T=int]:
main.cpp:23:13:从这里开始需要
main.cpp:8:5:错误:“struct std::enable_if”中没有名为“type”的类型
main.cpp:3:8:note:constexpr Foo::Foo(const Foo&)
main.cpp:3:8:注意:参数1从'int'到'const Foo&'没有已知的转换
main.cpp:3:8:note:constexpr Foo::Foo(Foo&&)
main.cpp:3:8:注意:参数1从'int'到'Foo&&'没有已知的转换
如果参考参数的
返回true,如何使启用\u?T
在这种情况下将永远不会被推断为参考类型。在对象u2
的构造中,构造函数模板参数被推断为int
虽然变量u2
的类型为int&
,但在表达式中使用u2
时,它是int
类型的左值表达式
模板参数推断使用函数参数的类型来推断模板参数类型。函数参数是表达式。因此,因为没有表达式具有引用类型,所以模板参数永远不会被推断为引用类型
[在C++11中,如果函数参数的类型为T&
,则如果参数为左值,T
可能会被推断为类型T&
。这种机制可以实现完美的转发。但这与您的场景无关。]
实际上,在表达式中,对象和对该对象的引用是不可区分的。引用只允许您为对象指定另一个名称。显而易见的答案是在is\u reference&!是积分的,与之相反,它是有效的,但不是一个好答案…@K-ballo我简化了我的问题,并将编辑问题以更准确地反映问题-这就是如何获得“启用”如果是有效的参考James,这是一段更复杂的代码的一部分,我试图使用enable_if
来区分整数、指针和l值引用。因此,使用enable\u if
,我想做的是可能的吗?不,因为。为什么您认为需要区分“对对象的引用”和“对象”?我正在尝试使用联合存储不同的数据类型-如果它是整型,我将值本身存储在union::uint64_t
成员中,如果它是指针,我将值本身存储在union::void*
成员中,如果它是引用,我将引用的地址存储在union::void*
成员中。我想重载union构造函数以使用模板参数推断来了解如何处理输入参数我不明白为什么要区别对待I
和l
l
只是i
的另一个名称。当你在一个表达式中使用这两者时,它们是不可区分的。我有一个并集,它在线程之间复制。为了防止过度复制,我想将可存储在union中的值限制为整数(最多64位)、指针或引用。我有一个静态断言,参数的大小最大为64位。目前,我可以存储指向大对象的指针,但不能存储引用(正是由于您所述的原因,参数被推断为T
,而不是T&
)。我希望能够推导出T&
参数,并存储union.ptr=&obj