constexpr

Is a constexpr more “constant” than const?

柔情痞子 提交于 2019-12-06 19:46:15
问题 The C++ Programming Language Fourth Edition - Bjarne Stroustrup: (emphasis mine) 2.2.3. Constants In a few places, constant expressions are required by language rules (e.g., array bounds (§2.2.5, §7.3), case labels (§2.2.4, §9.4.2), some template arguments (§25.2), and constants declared using constexpr). In other cases, compile-time evaluation is important for performance. Independently of performance issues, the notion of immutability (of an object with an unchangeable state) is an

Take the length of a string implicitly in templates

不想你离开。 提交于 2019-12-06 18:57:24
Ok, suppose I have this magic template that takes a char array and turns it into a template parameter list. For example: "thing" is translated to list<'t','h','i','n','g'> : template<const char*, size_t> ArrayToList; You use it like this: constexpr char string[] = "this is a test"; typedef ArrayToList<string,14> list; Is there any way to somehow detect the string length implicitly so that all I have to do is: typedef ArrayToList<string> list; Unfortunately, it can't be done: You would like to have a non-type template parameter (the string). Syntactically speaking, the only way to specify a non

Why must non-integral static data members initialized in the class be constexpr?

主宰稳场 提交于 2019-12-06 18:33:28
问题 Static integral data members initialized in the class definition may be declared const or constexpr , but non-integral static data members initialized in the class definition must be constexpr : class MyClass { static const int w = 5; // okay static constexpr int x = 5; // okay static const float y = 1.5; // error! static constexpr float z = 1.5; // okay }; Does anybody know why the declaration for y is not permitted? The part of the Standard making it illegal is 9.4.2/3, but why is it

Proper initialization of static constexpr array in class template?

不问归期 提交于 2019-12-06 17:21:52
问题 Static class members in C++ have caused a little confusion for me due to the standard's verbiage: 9.4.2 Static data members [class.static.data] The declaration of a static data member in its class definition is not a definition... However a constexpr is required to be initialized (AFAIK, couldn't find a quote from the standard) at its declaration (e.g., in the class definition). Because of the restrictions on constexpr I had actually forgotten about the requisite for static members to be

In C++11 is sqrt defined as constexpr?

放肆的年华 提交于 2019-12-06 16:30:29
问题 In C++11 is std::sqrt defined as constexpr , i.e. can it legally be used from other constexpr functions or in compile-time contexts like array sizes or template arguments? g++ seems to allow it (using -std=c++0x ), but I'm not sure I can take that as authoritative given that c++0x/c++11 support is still incomplete. The fact that I can't seem to find anything on the Internet makes me unsure. It seems like this should be something one could easily find out using Google, but I've tried (for 40

is bit_cast without compiler support for constexpr memcpy possible?

[亡魂溺海] 提交于 2019-12-06 14:54:51
I had heard that std::bit_cast will be in C++20, and I am slightly puzzled about the conclusion that implementing it necessarily requires special compiler support. To be fair, the argument I have heard is that the implementation performs a memcpy operation, and memcpy is not typically constexpr , while std::bit_cast is supposed to be, so making std::bit_cast constexpr supposedly requires compiler support for a constexpr -compliant memcpy operation. I was wondering, however, if it was possible to implement a compliant bit_cast (ie, defined behavior, to same extent that using memcpy would have

How do I make a constexpr swap function?

对着背影说爱祢 提交于 2019-12-06 13:57:27
I'm making my own String View class for learning purposes, and I'm trying to make it 100% constexpr. To test it, I have a member function that returns an hash value. I then construct my string view in a switch statement and call that same member function, if it passes, that member function has fullfiled its purpose. To learn, I'm using / reading / comparing my implementation with Visual Studio 2017 latest update std::string_view , however, I've noticed that, despite swap being marked as constexpr , it does not work, nor in Visual Studio, nor in g++. This is the piece of code that does not work

Returning a class from a constexpr function requires virtual keyword with g++

。_饼干妹妹 提交于 2019-12-06 13:26:53
Hi the following program works with g++ 4.9.2 (Ubuntu 4.9.2-10ubuntu13) but the virtual keyword is required for the function get : //g++ -std=c++14 test.cpp //test.cpp #include <iostream> using namespace std; template<typename T> constexpr auto create() { class test { public: int i; virtual int get(){ return 123; } } r; return r; } auto v = create<int>(); int main(void){ cout<<v.get()<<endl; } If I omit the virtual keyword, I get the following error : test.cpp: In instantiation of ‘constexpr auto create() [with T = int]’: test.cpp:18:22: required from here test.cpp:16:1: error: body of

Building OpenCV 3.2.0 with MinGW-w64 6.1.0: compile-time argument evaluation faiIure

久未见 提交于 2019-12-06 11:15:27
问题 The compiler fails with this output: In file included from C:/mingw-w64/x86_64-6.1.0-win32-seh-rt_v5-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/6.1.0/include/emmintrin.h:31:0, from C:/lib/opencv/sources/modules/core/include/opencv2/core/cvdef.h:168, from C:/lib/opencv/sources/modules/core/include/opencv2/core.hpp:52, from C:/lib/opencv/sources/modules/core/include/opencv2/core/utility.hpp:56, from C:/lib/opencv/sources/cmake-build-debug/modules/core/precomp.hpp:49: C:/lib/opencv/sources/modules

constexpr in C++11 and C++14 (not a difference to const keyword) [duplicate]

吃可爱长大的小学妹 提交于 2019-12-06 09:18:42
This question already has answers here : Difference between `constexpr` and `const` (8 answers) Closed 2 years ago . In the "Exploring C++17 and beyond" presentation by Mike Isaacson at one point ( https://youtu.be/-ctgSbEfRxU?t=2907 ) there is question about writing: const constexpr .... vs single const. Mike said that in C++11 constexpr implies const and in C++14 it does not. Is it true? I've tried to find prove for that but I couldn't. I'm not asking about difference between const and constexpr (as a lot of other questions) but about difference in constexpr in two versions of C++ standard.