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中?
我刚刚尝试使用clang 3.3和Ubuntu 13.04上的GCC 4.7.3标准库头文件编译相当大的代码.除了一个问题外,一切顺利.这段代码已经在这台机器上编译了标准的Ubuntu clang 3.2软件包,所以我假设这是clang 3.3编译器的一些变化.与使用复杂标头的const和constexpr相关的问题.特别是复杂类型具有以下代码块
#ifdef __GXX_EXPERIMENTAL_CXX0X__
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// DR 387. std::complex over-encapsulated.
constexpr double
real() { return __real__ _M_value; }
constexpr double
imag() { return __imag__ _M_value; }
#else
double&
real() { return __real__ _M_value; }
const double&
real() const { return __real__ _M_value; }
double&
imag() { return __imag__ _M_value; }
const double&
imag() const { return __imag__ _M_value; }
#endif
Run Code Online (Sandbox Code Playgroud)
在我的编译中,我输入第一个代码块,因此编译器可以看到
constexpr double real() { return __real__ _M_value; }
Run Code Online (Sandbox Code Playgroud)
这导致clang产生错误,实际成员函数不是const,具有以下内容
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/complex:1212:7:
note: candidate function …Run Code Online (Sandbox Code Playgroud) GNU C 和 C++ 提供const和pure函数属性。\n来自gnu 在线文档(重点是我的):
在 GNU C 和 C++ 中,您可以使用函数属性来指定某些函数属性,这些属性可以帮助编译器优化调用或更仔细地检查代码的正确性。例如,您可以使用属性来指定函数从不返回 (noreturn)、仅根据其参数的值返回值 (const)或具有 printf 样式参数 (format)。
\n其中 const 属性似乎是 pure 的子集,也取自gnu 文档:
\n\n\nconst 属性比类似的 pure 属性对函数\xe2\x80\x99s\n定义施加了更大的限制。诊断同时使用 const 和 pure 属性声明相同\n函数。
\n
在 C++ 11 中,添加了constexpr说明符。
当应用于函数时,const 属性和 constexpr 说明符之间有区别吗?GCC 是否应用了不同的优化?
\n一个听起来类似的问题是“constexpr”和“const”之间的差异。但我认为这不是重复的。我的问题具体是关于 function 属性const,它似乎与constexpr.
I'm delving into address constant expressions while reading "C++ Programming Language 4th edition" book. It has a short paragraph which describes address constant expressions:
The address of a statically allocated object, such as a global variable, is a constant. However, its value is assigned by the linker, rather than the compiler, so the compiler cannot know the value of such an address constant. That limits the range of constant expressions of pointer and reference type. For example:
Run Code Online (Sandbox Code Playgroud)constexpr const char* …
所以我写了这段代码->
#include <iostream>
#include <bitset>
int main(){
int num, temp, digits = 0;
std::cin >> num;
temp = num;
while(temp){
temp /= 10;
++digits;
}
const int size = digits;
std::bitset<size> a(num);
std::cout << a << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
位集容器不接受 const 整数大小作为参数并抛出错误 - Non-type template argument is not a constant expression。我想知道为什么会发生这种情况,因为大小已被声明为常量,并且它的值在程序运行时不会改变?
在C++中,如果变量的值在整个程序VS中分配后永远不会改变如果将该变量设为const,那么可执行代码更快?编译器如何优化案例1中的可执行代码?
在 C++17 中,像这样声明全局常量有什么区别:
namespace ns
{
static constexpr const auto global_variable = 47;
}
Run Code Online (Sandbox Code Playgroud)
还指定const修饰符,并且:
namespace ns
{
static constexpr auto global_variable = 47;
}
Run Code Online (Sandbox Code Playgroud)
没有指定const?如果是,有哪些差异以及在哪些场景下推荐使用哪种版本的声明?
看下面的代码,这const static int num1 = 8;可以编译。但是,const static double num2 = 8.8;代码给出了错误。它们的修饰符都是const static,那么为什么可以int工作,但是double其他浮点类型会导致错误呢?
struct S {
const static int num1 = 8; // OK
const static double num2 = 8.8; // error
};
Run Code Online (Sandbox Code Playgroud)
我尝试了一些 C++ 的内置类型,发现
int、 、short、long、 等整型成员可以在类中char定义并直接初始化,而const staticfloat和double不能。这让我很困惑。
我正在尝试了解在测试更改时我所看到的内容.该平台是带有GCC 4.8的openSUSE 42,但它可能会影响其他人.测试代码和错误如下.
$ cat test.cxx
#include <string>
#if (__cplusplus >= 201103L)
# define STATIC_CONSTEXPR static constexpr
# define CONSTEXPR constexpr
#else
# define STATIC_CONSTEXPR static const
# define CONSTEXPR
#endif
struct Name
{
STATIC_CONSTEXPR char* GetName() {return "XXX";}
};
int main(int argc, char* arv[])
{
const char* name = Name::GetName();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和:
$ g++ -O3 -std=c++11 test.cxx -o test.exe
test.cxx: In static member function ‘static constexpr char* Name::GetName()’:
test.cxx:13:44: warning: deprecated conversion from string constant to ‘char*’ …Run Code Online (Sandbox Code Playgroud) 出于某种原因,我很难掌握如何正确使用constexpr.
标题中描述的情况是否适合使用它?即:
void foo()
{
static constexpr const size_t MAX_BUFFER_SIZE = 20 * 1024 * 1024;
constexpr size_t bufferSize = 1024 * 1024; // Initialized with constant expression
std::vector<char> buffer(bufferSize, ' ');
//...
if (some_condition())
{
bufferSize = get_random_value_at_runtime(); // Assigned a new 'non-constexpr' value
buffer.resize(bufferSize, ' ');
}
//...
}
Run Code Online (Sandbox Code Playgroud)
亲切的问候!
谢谢,我不需要任何书来教我什么constexpr意思.我正在教学constexpr,我的简单例子无法说服学生为什么应该利用编译时计算的优势constexpr.
还请严格避免链接到问题,比如这个它没有汇编代码或分析,他们是没有意义的,我的问题.
我正在寻找一个例子来说明为什么constexpr它有用并且不能被解雇.
好吧,在很多情况下如果constexpr被替换为const没有错,确实会发生.所以,我设计了以下示例:
main_const.cpp
#include <iostream>
using namespace std;
const int factorial(int N)
{
if(N<=1)
return 1;
else
return N*factorial(N-1);
}
int main()
{
cout<<factorial(10)<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和
main_constexpr.cpp
#include <iostream>
using namespace std;
constexpr int factorial(int N)
{
if(N<=1)
return 1;
else
return N*factorial(N-1);
}
int main()
{
cout<<factorial(10)<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但问题是,对于他们以前的那个,汇编代码是
main_const.asm
12:main_last.cpp **** int main()
13:main_last.cpp **** {
132 .loc …Run Code Online (Sandbox Code Playgroud) c++ ×10
constexpr ×8
c++11 ×5
const ×2
constants ×2
bitset ×1
c ×1
c++14 ×1
equivalent ×1
gcc ×1
performance ×1
pointers ×1
static ×1
std-bitset ×1