constexpr

Can a string literal be subscripted in a constant expression?

馋奶兔 提交于 2019-11-26 23:13:41
问题 This is valid, because a constexpr expression is allowed to take the value of "a glvalue of literal type that refers to a non-volatile object defined with constexpr, or that refers to a sub-object of such an object" (§5.19/2): constexpr char str[] = "hello, world"; constexpr char e = str[1]; However, it would seem that string literals do not fit this description: constexpr char e = "hello, world"[1]; // error: literal is not constexpr 2.14.5/8 describes the type of string literals: Ordinary

Why does constexpr static member (of type class) require a definition?

删除回忆录丶 提交于 2019-11-26 23:03:52
问题 ==> See the full snippet code and compilation on coliru. I have a LiteralType class filling constexpr requirements: struct MyString { constexpr MyString(char const* p, int s) : ptr(p), sz(s) {} constexpr char const* data() const { return ptr; } constexpr int size() const { return sz; } char const *ptr = 0; int const sz = 0; }; I use it as a constexpr static member variable: struct Foo { int size() { return str_.size(); } constexpr static MyString str_{"ABC",3}; }; int main() { Foo foo; return

Concat two `const char` string literals

☆樱花仙子☆ 提交于 2019-11-26 22:41:41
Is it possible to concat two string literals using a constexpr ? Or put differently, can one eliminate macros in code like: #define nl(str) str "\n" int main() { std::cout << nl("usage: foo") nl("print a message") ; return 0; } Update : There is nothing wrong with using "\n" , however I would like to know whether one can use constexpr to replace those type of macros. Yes, it is entirely possible to create compile-time constant strings, and manipulate them with constexpr functions and even operators. However, The compiler is not required to perform constant initialization of any object other

c++11 constexpr flatten list of std::array into array

浪尽此生 提交于 2019-11-26 22:41:03
问题 I am beginning with c++11, constexpr and template metaprogramming seems a nice way to save scarce ram on tiny microcontroler. Is there a way to write a template to flatten a list of constexpr array, what I need is a way to do : constexpr std::array<int, 3> a1 = {1,2,3}; constexpr std::array<int, 2> a2 = {4,5}; constexpr auto a3 = make_flattened_array (a1,a2); I use gcc 4.8.4 (arm-none-eabi), and can compile with std=c++11 or c++1y option if it is needed. 回答1: Notice - I understood your

Is a constexpr array necessarily odr-used when subscripted?

风格不统一 提交于 2019-11-26 22:31:38
Given the following code: struct A { static constexpr int a[3] = {1,2,3}; }; int main () { int a = A::a[0]; int b [A::a[1]]; } is A::a necessarily odr-used in int a = A::a[0] ? Note: This question represents a less flamey/illogical/endless version of a debate in the Lounge . First use of A::a : int a = A::a[0]; The initializer is a constant expression, but that doesn't stop A::a from being odr-used here. And, indeed, A::a is odr-used by this expression. Starting from the expression A::a[0] , let's walk through [basic.def.odr](3.2)/3 (for future readers, I'm using the wording from N3936): A

Initializing a static constexpr from an incomplete type because of a template base class

隐身守侯 提交于 2019-11-26 21:56:34
问题 I have a template base class, whereby subclasses are expected to pass themselves as the template parameter. It looks a little like this: template<typename T> struct Base { constexpr Base(int x) : m_x(x) {} private: int m_x; }; struct Derived : public Base<Derived> { static const Derived LIFE; constexpr Derived(int x) : Base(x) {} }; const Derived Derived::LIFE = Derived(42); That compiles and works as expected. But now I'd like to make Derived::LIFE a constexpr. Is this even possible? I can't

Error using a constexpr as a template parameter within the same class

血红的双手。 提交于 2019-11-26 21:51:18
问题 If I try to compile the following C++0x code, I get an error: template<int n> struct foo { }; struct bar { static constexpr int number() { return 256; } void function(foo<number()> &); }; With gcc 4.6.1, the error message is: test.cc:6:27: error: ‘static constexpr int bar::number()’ used before its definition test.cc:6:28: note: in template argument for type ‘int’ With clang 2.8, the error message is: test.cc:6:20: error: non-type template argument of type 'int' is not an integral constant

Is constexpr a “hint” (like inline) or “a binding request” to the compiler?

依然范特西╮ 提交于 2019-11-26 21:10:01
问题 Is constexpr an indicator for the compiler or does it mandate a behaviour ? The example at hand is the following : template<typename T> std::size_t constexpr getID() { return typeid(T).hash_code(); } hash_code is a runtime constant, yet this snippet would compile even though a compile time evaluation is requested with constexpr . Only after the return value is used where a compile time constant is expected, would we get noticed that this is not usable as a constexpr function. So is constexpr

How to ensure constexpr function never called at runtime?

筅森魡賤 提交于 2019-11-26 20:55:02
问题 Lets say that you have a function which generates some security token for your application, such as some hash salt, or maybe a symetric or asymetric key. Now lets say that you have this function in your C++ as a constexpr and that you generate keys for your build based on some information (like, the build number, a timestamp, something else). You being a diligent programmer make sure and call this in the appropriate ways to ensure it's only called at compile time, and thus the dead stripper

Detecting constexpr with SFINAE

大城市里の小女人 提交于 2019-11-26 20:24:09
I'm working on upgrading some C++ code to take advantage of the new functionality in C++11. I have a trait class with a few functions returning fundamental types which would most of the time, but not always, return a constant expression. I would like to do different things based on whether the function is constexpr or not. I came up with the following approach: template<typename Trait> struct test { template<int Value = Trait::f()> static std::true_type do_call(int){ return std::true_type(); } static std::false_type do_call(...){ return std::false_type(); } static bool call(){ return do_call(0