我最近看过这个视频,它解释了C++中的Concepts Lite的思想,这些思想很可能会在今年作为TS出现。现在,我也了解到关于万能引用/转发引用的知识(如这里所描述),以及T&&在不同上下文中有两种含义的情况(即是否进行类型推导)。这自然地引出了一个问题,那就是Concepts和万能引用之间将如何交互?
为了具体说明,我们可以看下面的例子:
void f(int&& i) {}
int i = 0;
f(i); // error, looks for f(int&)
f(0); // fine, calls f(int&&)
并且
template <typename T>
void f(T&& test) {}
int i = 0;
f(i); // fine, calls f(T&&) with T = int& (int& && = int&)
f(0); // fine, calls f(T&&) with T = int&& (int&& && = int&&)
但是如果我们使用概念会发生什么?
template <typename T>
requires Number<T>
void f(T&& test) {}
template <Number T>
void g(T&& test) {}
void h(Number&& test) {}
int i = 0;
f(i); // probably should be fine?
f(0); // should be fine anyway
g(i); // probably also fine?
g(0); // fine anyway
h(i); // fine or not?
h(0); // fine anyway
特别是最后一个例子让我有些困扰,因为存在两个相互冲突的原则。首先,以这种方式使用的概念应该像类型一样工作;其次,如果T是一个推导类型,则T&&表示通用引用而不是右值引用。
感谢您提前对此进行澄清!
T
将是int
,而不是int&&
。 - palapapaT
应该是int
,而不是int&&
。 - undefined