是否可以将类型别名定义为constexpr函数
Is it possible to define type alias to constexpr function
在C 11或C 14中,我正在尝试将类型的别名定义为constexpr函数。
我尝试了:
#include <iostream>
constexpr int foo(int i, int j) { return i + j; }
using TConstExprFunction = constexpr int (*)(int i, int j);
int main() {
TConstExprFunction f = foo;
constexpr int i = f(1, 2);
std::cout << i << std::endl;
}
,但无法与G 和Clang 进行编译。
g : error: expected type-specifier before 'constexpr'
clang : error: type name does not allow constexpr specifier to be specified
我必须在下面做才能使其编译
#include <iostream>
constexpr int foo(int i, int j) { return i + j; }
using TConstExprFunction = int (*)(int i, int j);
int main() {
constexpr TConstExprFunction f = foo;
constexpr int i = f(1, 2);
std::cout << i << std::endl;
}
来自clang 的错误消息,看来我不能将 constexpr
用于类型名称。
so,可以将类型的别名定义为constexpr函数。如果是,如何?
根据C 标准 7.1.5/p8 constexpr
指定器[dcl.constexpr] (重点是Mine ):
constexpr
指示符对constexpr
的类型没有影响 功能或constexpr
构造函数。
也来自 7声明[dcl.dcl] :
alias-declaration: using identifier attribute-specifier-seqopt = defining-type-id ;
constexpr
指示符不是函数类型的一部分。因此,您不能做:
using TConstExprFunction = constexpr int (*)(int i, int j);
因为在using TConstExprFunction =
之后,预期是一种类型。
您不能为constexpr函数定义类型别名。
相关文章:
- 通过 lambda 定义的 constexpr 递归函数
- 将预处理器配置定义迁移到 constexpr,而不会造成内存膨胀
- 如何将用户定义的文字放在C++相同类型的 constexpr 类中?
- 我可以定义一个 constexpr 匿名/未命名变量吗?
- 使用模板参数还包括 constexpr 成员函数enable_if单独定义和声明模板成员函数
- 在模板定义中调用非静态constexpr成员函数
- 编译器如何知道C++constexpr计算不会触发未定义的行为
- 有没有办法声明一个公共静态常量,该常量将使用 constexpr 在源文件中定义(有什么区别)?
- constexpr 用户定义的文字:允许吗?
- 为什么 #pragma 一次不防范多个非 constexpr 定义?
- constexpr 和未定义的行为
- 现在允许重新定义 constexpr 静态数据成员吗?(但不是内联常量)?
- 文件中定义的 Constexpr 构造函数.cpp会导致链接错误
- 定义 constexpr 静态数据成员
- 通过呼叫constexpr函数来定义静态constexpr成员
- constexpr:constexpr成员的定义和声明
- 在定义(.cpp文件)中初始化静态浮点 constexpr 成员是可能的
- C++11 - 无法使用 constexpr 函数定义 constexpr 文字?
- VC++编译器升级2010->2015重新定义; 'constexpr'说明符不匹配
- Clang抱怨在未求值的上下文中没有定义constexpr函数