【发布时间】:2017-03-12 23:50:06
【问题描述】:
是否可以使用 typedef 或 using 在概念内声明类型别名,如概念 TS 所建议的那样?
如果我尝试类似以下 MWE,则代码无法编译(使用 gcc 6.2.1 和 -fconcepts 开关)
#include <type_traits>
template<typename T>
concept bool TestConcept ()
{
return requires(T t)
{
using V = T;
std::is_integral<V>::value;
};
}
int main()
{
return 0;
}
产生的错误:
main.cpp: In function ‘concept bool TestConcept()’:
main.cpp:8:9: error: expected primary-expression before ‘using’
using V = T;
^~~~~
main.cpp:8:9: error: expected ‘}’ before ‘using’
main.cpp:8:9: error: expected ‘;’ before ‘using’
main.cpp:4:14: error: definition of concept ‘concept bool TestConcept()’ has multiple statements
concept bool TestConcept ()
^~~~~~~~~~~
main.cpp: At global scope:
main.cpp:11:1: error: expected declaration before ‘}’ token
}
^
【问题讨论】:
-
您似乎想使用
typedef V T;,这会将T别名为V。using用于调用命名空间或命名空间中的特定标识符。这是一个示例:stackoverflow.com/questions/10103453/… -
@JamesMurphy 抱歉,但是从 c++11 开始,您可以使用
using关键字来表达类型别名,就像您之前使用typedef所做的那样。这是参考:en.cppreference.com/w/cpp/language/type_alias. -
@JamesMurphy 该示例也因 typedef 失败,基本上具有相同的错误消息。正如 erikzenker 所说,现在的语法应该是等价的。
-
我没有使用足够多的 C++11 来了解这些细微差别,但我想我会去寻找有关该主题的东西。如果语法相同,请尝试改用
typedef。
标签: c++ typedef c++-concepts using-declaration