我一直在constexpr研究C++ 的新功能,但我并不完全理解它的必要性.
例如,以下代码:
constexpr int MaxSize()
{
...
return ...;
}
void foo()
{
int vec[MaxSize()];
}
Run Code Online (Sandbox Code Playgroud)
可以替换为:
int MaxSize()
{
...
return ...;
}
static const int s_maxSize = MaxSize();
foo()
{
int vec[s_maxSize];
}
Run Code Online (Sandbox Code Playgroud)
更新
第二个例子实际上不是标准的ISO C++(感谢几个用户指出这一点),但某些编译器(例如gcc)支持它.因此,这不是const使程序有效,而是gcc支持这种非标准功能的事实.(据我所知,只有当数组被定义为函数或方法的本地数据时才有可能,因为在编译时必须知道全局数组的大小.)如果我编译没有选项-std=c++98 -pedantic-errors,甚至代码
int MaxSize()
{
return 10;
}
void foo()
{
int vec[MaxSize()];
}
Run Code Online (Sandbox Code Playgroud)
将使用gcc编译.
因此,考虑到目前为止的反馈(以及我在同一时间进行的一些进一步阅读),我将尝试重新解释我的问题.
我const大量使用关键字.随着const我可以定义有其整个生命周期内某一特定值的常数.可以使用任何表达式初始化常量,该表达式被计算一次,即创建常量时.对于这些情况,我认为这constexpr是非常无用的:它将引入一个非常小的优化,因为定义常量值的表达式将在编译时而不是运行时计算.每次我需要一个复杂初始化的运行时常量时我都会使用关键字const.
因此constexpr,在我们需要在编译时初始化常量的情况下,可能会派上用场.一个示例是矢量定义:标准不支持在运行时定义大小.另一个示例是具有一个或多个非类型参数的模板.
在这种情况下,我通常使用宏:
#define MAX_SIZE (10)
void foo()
{
int …Run Code Online (Sandbox Code Playgroud) 所以我读了关于constexpr 和 const 之间有什么区别的有趣答案,但我很好奇 #define 和 constexpr 之间有什么区别?我觉得 constexpr 只是一个可以选择类型的#define。
以下代码无法在Ideone上实时编译:
#include <iostream>
using namespace std;
int main() {
const double kPi = 3.14;
constexpr double kPi2 = 2.0*kPi;
cout << kPi2;
}
Run Code Online (Sandbox Code Playgroud)
错误消息是:
Run Code Online (Sandbox Code Playgroud)prog.cpp: In function 'int main()': prog.cpp:6:30: error: the value of 'kPi' is not usable in a constant expression constexpr double kPi2 = 2.0*kPi; ^ prog.cpp:5:15: note: 'kPi' was not declared 'constexpr' const double kPi = 3.14;
将const声明替换为kPiwith constexpr,它会成功编译.
在另一方面,如果int是用来代替double,好像const 还有戏剧与constexpr …
以下代码在gcc 4.8和Clang 3.2下编译:
int main()
{
int size = 10;
int arr[size];
}
Run Code Online (Sandbox Code Playgroud)
C++标准的8.3.4/1表示数组的大小必须是一个整数常量表达式,这size似乎不是.这是两个编译器中的错误,还是我错过了什么?
最新的VC++ CTP用这个有趣的消息拒绝代码:
error C2466: cannot allocate an array of constant size 0
Run Code Online (Sandbox Code Playgroud)
有趣的是,它似乎认为size是零.但至少它拒绝了代码.gcc和Clang应该不一样吗?
c++ arrays compile-time-constant variable-length-array c++11
#include <iostream>
struct Index {
constexpr operator int() const { return 666; }
};
template <int i> void foo() {
std::cout << i << std::endl;
}
void wrapper(Index index) {
foo<index>();
}
int main() {
Index index;
// foo<index>(); // error: the value of ‘index’ is not usable in a constant expression
wrapper(index);
}
Run Code Online (Sandbox Code Playgroud)
大家好.
我正在使用变量"index"的constexpr转换为int值,该值替换为"foo"模板化函数.如果我直接foo<index>()从"main" 调用,我会收到编译器错误.如果从"包装器"完成相同的调用,那么一切都编译并正常工作.
我在那里错过了什么?
编译命令:g++ -std=c++14 main.tex使用g ++(GCC)5.3.1 20160406(Red Hat 5.3.1-6).
我有以下代码:
static constexpr const char*const myString = "myString";
Run Code Online (Sandbox Code Playgroud)
你能解释一下有什么区别:
static const char*const myString = "myString";
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们与constexpr有什么新鲜事?
这些成员变量之间有什么区别:
struct my_class {
static const int i = 0;
static constexpr int j = 0;
};
Run Code Online (Sandbox Code Playgroud)
如果我的理解是正确的,我可以使用它们i和j编译时常量.也就是说,无论是std::array<int, my_class::i>和std::array<int,my_class::j>应该工作.
返回整数文字副本的函数
int number()
{ return 1; }
Run Code Online (Sandbox Code Playgroud)
可以使用关键字轻松转换为普通的编译时表达式constexpr.
constexpr int number()
{ return 1; }
Run Code Online (Sandbox Code Playgroud)
但是,当涉及到字符串文字时,我会感到困惑.通常的方法是返回指向const char字符串文字的指针,
const char* hello()
{ return "hello world"; }
Run Code Online (Sandbox Code Playgroud)
但我认为仅仅改变"const" constexpr并不是我想要的(作为奖励,它还会产生编译器警告,使用gcc 4.7.1 将不推荐的从字符串常量转换为'char*')
constexpr char* hello()
{ return "hello world"; }
Run Code Online (Sandbox Code Playgroud)
有没有办法以hello()这样的方式实现调用在下面的示例中用常量表达式替换?
int main()
{
std::cout << hello() << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我知道constexpr 变量可以在编译时使用.例如,对于模板或静态asser.
但如果我想在没有constexpr的情况下这样做,我可以static const.
什么是自C++ 11/14引入constexpr之间的区别
constexpr int a = 3;
//AND
static const int a = 3;
Run Code Online (Sandbox Code Playgroud)
谢谢!
另一种看待这个问题的方法是我应该使用哪种方法?
cppreference指出:
在对象声明或非静态成员函数中使用的constexpr说明符(直到C++ 14)暗示const.
"对象声明"是否意味着"任何变量声明"?
即
constexpr const int someConstant = 3;
Run Code Online (Sandbox Code Playgroud)
相当于
constexpr int someConstant = 3;
Run Code Online (Sandbox Code Playgroud)
在C++ 11,C++ 14和C++ 17中?