C++ 概念精简版和类型别名声明 [英] C++ concepts lite and type alias declaration

查看:26
本文介绍了C++ 概念精简版和类型别名声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 typedefusing 在概念内声明类型别名,如概念 TS 所提议的那样?如果我尝试类似下面的 MWE,代码不会编译(使用 gcc 6.2.1 和 -fconcepts 开关)

Is it possible to use typedef or using to declare a type alias inside a concept, as proposed by the Concepts TS? If I try something like the following MWE, the code does not compile (with gcc 6.2.1 and the -fconcepts switch)

#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
 } 
 ^

推荐答案

没有.根据概念 TS,一个要求是:

No. According to the concepts TS, a requirement is:

要求:
   简单要求
   类型要求
   复合要求
   嵌套需求

其中 simple-requirement 是一个 表达式 后跟一个 ; 和一个 type-requirement比如typename T::inner.另外两个听起来就像名字所暗示的那样.

Where a simple-requirement is an expression followed by a ; and a type-requirement is something like typename T::inner. The other two sound like what the name suggests.

类型别名是声明,而不是表达式,因此不符合要求的要求.

A type alias is a declaration, not an expression, and so does not meet the requirement of a requirement.

这篇关于C++ 概念精简版和类型别名声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆