constexpr

constexpr with string operations workaround?

﹥>﹥吖頭↗ 提交于 2019-12-03 17:41:28
This previously answered question explains why the code I have posted below does not work. I have a follow-up question: is there a workaround that is conceptually equivalent, i.e., achieves compile-time string concatenation, but is implemented in a way that is actually supported by C++11? Using std::string is completely non-essential. constexpr std::string foo() { return std::string("foo"); } constexpr std::string bar() { return std::string("bar"); } constexpr std::string foobar() { return foo() + bar(); } Compile-time "string" concatenation : #include <iostream> #include <string> template

Constexpr variable evaluation

北城以北 提交于 2019-12-03 17:06:14
Here is my code and I need clarification on what's happening: constexpr int funct(int x){ return x + 1; } int main(){ int x = funct(10); return 0; } constexpr 's allows compile time calculation, and based on my code above, since funct is declared as constexpr , it is allowed to do compile time calculations if the arguments are constants or constexpr's themselves. The part that I am confused with lies in this part, the int x . Since it is not declared as constexpr , would it mean that int x will get the value at runtime? Does that mean that declaring it to be constexpr int x will mean that int

constexpr, arrays and initialization

这一生的挚爱 提交于 2019-12-03 16:41:23
Is there anything in the world of C++ that would make what I'm trying to do possible? template < typename T , size_t Size > struct array { constexpr T buf[Size]; constexpr size_t size() const { return Size; } }; template < typename T , size_t Size > constexpr array<T,Size+1> push_back(array<T,Size> const& arr, T const& val) { array<T,Size+1> arr_out = {{arr.buf, val}}; return arr_out; } What I'm trying to do is create a new array initialized with the data in the other, and put a new element on the end. Minus the constexpr I can get it to work by loop initializing in the push_back function. It

why declare constrexpr constructors for classes with non-trivial destructors (e.g. unique_ptr, std::variant)

末鹿安然 提交于 2019-12-03 16:36:49
问题 As far as I understand (at least for c++14 ), a destructor cannot be constexpr if it is not trivial (implicit generated or =default ). What is the point of declaring constexpr constructors for structures with non-trivial destructors? struct X { int a_; constexpr X(int a) : a_{a} {} // constexpr ~X(){}; // Error dtor cannot be marked constexpr // ~X(){}; // causes error at y declaration: temporary of non-literal type ‘X’ // in a constant expression . }; template <int N> struct Y {}; int main()

Modern C++: initialize constexpr tables

折月煮酒 提交于 2019-12-03 16:17:11
问题 Suppose I have a class X , which functionality requires a lot of constant table values, say an array A[1024] . I have a recurrent function f that computes its values, smth like A[x] = f(A[x - 1]); Suppose that A[0] is a known constant, therefore the rest of the array is constant too. What is the best way to calculate these values beforehand, using features of modern C++, and without storaging file with hardcoded values of this array? My workaround was a const static dummy variable: const bool

Difference between constexpr and static constexpr global variable

淺唱寂寞╮ 提交于 2019-12-03 15:44:15
问题 In the C++11 standard, what is the difference between constexpr and static constexpr global variables when defined in a header? More specifically, when multiple translation units include the same header, which declaration (if any) is guaranteed to define the same variable across the translation units? e.g., cexpr.h: #ifndef CEXPR_H #define CEXPR_H constexpr int cint = 1; static constexpr int scint = 1; #endif a.cpp: #include "cexpr.h" b.cpp: #include "cexpr.h" 回答1: In your current example

Global constants in C++11

你说的曾经没有我的故事 提交于 2019-12-03 15:33:29
问题 What are the best ways to declare and define global constants in C++? I am mostly interested in C++11 standard as it fixes a lot in this regard. [EDIT (clarification)]: in this question "global constant" denotes constant variable or function that is known at compile time in any scope. Global constant must be accessible from more than one translation unit. It is not necessarily constexpr-style constant - can be something like const std::map<int, std::string> m = { { 1, "U" }, { 5, "V" } }; or

What is constexpr in C++?

隐身守侯 提交于 2019-12-03 15:31:43
问题 I am really confused about a constexpr concept, as I have read constexpr is evaluated at compile time, so it is useful for performance optimization versus normal const . constexpr int i = 0; constexpr int& ri = i; The above code returns an error "invalid initialization of reference of type 'int&' from expression of type 'const int'", why? Also, the next code has an error: constexpr int i = 0; constexpr int* ri = &i; If I replaced the constexpr keyword with const , all above worked correctly.

Using an int as a template parameter that is not known until run-time

只愿长相守 提交于 2019-12-03 14:32:28
I am trying to use an integer as a template parameter for a class. Here is a sample of the code: template< int array_qty > class sample_class { public: std::array< std::string, array_qty > sample_array; } If I do so something like this, it works: sample_class< 10 > sample_class_instance; However, let's say that I do not know the value of array_qty (the template parameter) when compiling, and will only know it during run-time. In this case, I would essentially be passing an int variable as the template argument. For the sake of demonstration, the following code does not work: int test_var = 2;

Constexpr and SSE intrinsics

陌路散爱 提交于 2019-12-03 12:56:16
Most C++ compilers support SIMD(SSE/AVX) instructions with intrisics like _mm_cmpeq_epi32 My problem with this is that this function is not marked as constexpr , although "semantically" there is no reason for this function to not be constexpr since it is a pure function. Is there any way I could write my own version of (for example) _mm_cmpeq_epi32 that is constexpr ? Obviously I would like that the function at runtime uses the proper asm, I know I can reimplement any SIMD function with slow function that is constexpr . If you wonder why I care about constexpr of SIMD functions. Non