为什么我不能递增简单 constexpr 函数的参数?
Why I can't increment the parameter of the simple constexpr function?
Visual Studio 2015更新3。
我读了编程。Bjarne Stroustrup的《使用C++的原理与实践》(第二版)。我学习了constexpr
函数。。。
它起作用:
constexpr int get_value(int n) {
return n + 1;
}
但我无法编译这个(而不是第一个变体):
constexpr int get_value(int n) {
return ++n;
}
我得到错误:
constexpr函数返回的是非常量
n
是get_value
函数的局部变量。即n
变量的变化对外部代码没有影响。
为什么get_value
函数的第二个变体是错误的?
第二个在C++11 constexpr
中是不允许的。该标准甚至有一个非常相似的例子(N3337[dcl.constexpr]/3):
constexpr int prev(int x)
{ return --x; } // error: use of decrement
N3337[expr.const]/2明确禁止常量表达式中的"递增或递减操作"。
C++14扩展的constexpr
放宽了这些要求,但MSVC没有实现这些要求。
第二个在C++14下是合法的,但它不能编译,因为Visual Studio 2015只部分支持constexpr
函数。它只支持单返回constexpr
函数和其他限制(如您的),这些限制在C++11中有效。
请参阅本文(constexpr
段)。Visual Studio"15"将对constexpr
功能进行改进。您需要等待一段时间:)
相关文章:
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 当从函数参数中的临时值调用复制构造函数时
- 如何从"decltype()"获取函数参数的数量<funtion>?
- 如何将lambda作为模板类的成员函数参数
- 模板参数推导失败,函数参数/参数不匹配
- 如何在C++中将迭代器作为函数参数传递
- 将函数参数"const char*"转换为"std::string_view"是
- C++ 如何将数组值解压缩为函数参数
- 主函数参数的属性
- 具有两个间接寻址运算符 (C++) 的函数参数的用途
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 如何定义在用作函数参数时工作的类模板的转换
- 将函数参数完美转发到函数指针:按值传递呢?
- 为什么我不能将引用作为 std::async 的函数参数传递
- 什么..(省略号)作为函数原型中唯一的函数参数,C++?
- 是否可以就地构造一个固定大小的数组作为函数参数?
- 接受模板作为函数参数
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- Arduino 函数参数