constexpr

How to write constexpr swap function to change endianess of an integer?

送分小仙女□ 提交于 2019-12-03 03:47:12
问题 How to write a constexpr function to swap endianess of an integer, without relying on compiler extensions and can you give an example on how to do it? 回答1: Yes, it's pretty easy; here's a recursive (C++11-compatible) implementation (unsigned integral types only): #include <climits> #include <cstdint> #include <type_traits> template<class T> constexpr typename std::enable_if<std::is_unsigned<T>::value, T>::type bswap(T i, T j = 0u, std::size_t n = 0u) { return n == sizeof(T) ? j : bswap<T>(i >

Cannot construct constexpr array from braced-init-list

淺唱寂寞╮ 提交于 2019-12-03 02:10:15
I've implemented a constexpr array like this: template <typename T> class const_array { const T* p; unsigned n; public: template <unsigned N> constexpr const_array(const T(&a)[N]): p(a), n(N) { } constexpr unsigned size() const { return n; } }; int main(int argc, char* argv[]) { // works static_assert(const_array<double>{{1.,2.,3.}}.size() == 3); // doesn't compile constexpr const_array<double> a{{1.,2.,3.}}; static_assert(a.size() == 3); } Why is it that the first static_assert compiles, but initializing a fails?I'm using gcc 6.2.0. I'm getting : In function 'int main(int, char**)': : error:

How do I check if a template parameter is a power of two?

旧巷老猫 提交于 2019-12-03 01:54:07
I want to create a structure that allocates statically an array of 2^N bytes , but I don't want the users of this structure to specify this size as the exponent. Example: my_stupid_array<char, 32> a1; // I want this! my_stupid_array<char, 5> a2; // And not this... How do I check if this template parameter is a power of two and warn the user with a nice message about this? I've been able to check for this with a simple template: template<int N> struct is_power_of_two { enum {val = (N >= 1) & !(N & (N - 1))}; }; However, I'm unable to warn the user about this with a sane message. Any ideas? EDIT

Determine `constexpr` execution - during compilation or at runtime?

谁都会走 提交于 2019-12-02 23:07:53
Is there a way to achieve different behaviour of a constexpr function in the compilation phase and at runtime? Consider the following example (using a theoretical feature from D: static if ): constexpr int pow( int base , int exp ) noexcept { static if( std::evaluated_during_translation() ) { auto result = 1; for( int i = 0 ; i < exp ; i++ ) result *= base; return result; } else { // std::evaluated_during_runtime() return std::pow( base , exp ); } } If not, is there a way to restrict constexpr to be compile-time only? No, there is no such way. Sorry. N3583 is a paper proposing changes to allow

Why do we need the two definitions: integral constant expression and converted constant expression?

这一生的挚爱 提交于 2019-12-02 22:35:52
§5.19/3 in C++14 defines an integral constant expression and a converted constant expression: An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression. [ Note: Such expressions may be used as array bounds (8.3.4, 5.3.4), as bit-field lengths (9.6), as enumerator initializers if the underlying type is not fixed (7.2), and as alignments (7.6.2). —end note ] A converted constant expression of type T is an expression, implicitly converted to a prvalue of type T , where

Does specifying constexpr on constructor automatically makes all objects created from it to be constexpr?

冷暖自知 提交于 2019-12-02 18:49:36
Here is my code: class test{ public: constexpr test(){ } constexpr int operator+(const test& rhs){ return 1; } }; int main(){ test t; //constexpr word isn't necessary constexpr int b = t+test(); // works at compile time! int w = 10; // ERROR constexpr required constexpr int c = w + 2; // Requires w to be constexpr return 0; } I notice that it worked even though I didn't specify test to be constexpr . I tried replicating the result by doing the same with int but i get errors. Specifically, it wants my int w inside the constexpr int c = w + 2; to be constexpr . From my first attempt which is

What is the function parameter equivalent of constexpr?

核能气质少年 提交于 2019-12-02 09:19:15
问题 We are trying to speedup some code under Clang and Visual C++ (GCC and ICC is OK). We thought we could use constexpr to tell Clang a value is a compile time constant but its causing a compile error: $ clang++ -g2 -O3 -std=c++11 test.cxx -o test.exe test.cxx:11:46: error: function parameter cannot be constexpr unsigned int RightRotate(unsigned int value, constexpr unsigned int rotate) ^ 1 error generated. Here is the reduced case: $ cat test.cxx #include <iostream> unsigned int RightRotate

Is constexpr useful for overload

好久不见. 提交于 2019-12-02 05:28:32
问题 Is there a way in c++ to get a different overload called based on the runtime/compile time constness of an input? My version(12) of MSVC can't do this using constexpr. Reading c++ documentation, I am not sure if this is the way constexpr works. inline int Flip4(constexpr int n) { return ((n & 0xFF) << 24) | ((n & 0xFF00) << 8) | ((n & 0xFF0000) >> 8) | ((n & 0xFF000000) >> 24); } inline int Flip4(int n) { return _byteswap_ulong(n); } int main(int argc, char* argv[]) { int a = Flip4('abcd'); /

Why do templates allow constexpr function members with non-constexpr constructors?

本小妞迷上赌 提交于 2019-12-02 05:06:23
问题 Using C++14. Why will this compile: template<unsigned N> constexpr bool foo() { std::array<char, N> arr; return true; } but not this? constexpr bool foo() { std::array<char, 10> arr; // Non-constexpr constructor 'array' cannot be used in a constant expression return true; } 回答1: §7.1.5 [dcl.constexpr]/p6: If the instantiated template specialization of a constexpr function template or member function of a class template would fail to satisfy the requirements for a constexpr function or

Trying to pass a constexpr lambda and use it to explicitly specify returning type

走远了吗. 提交于 2019-12-02 04:57:51
问题 I would like to use a function and pass a constexpr lambda . However, it only compiles successfully if I let the type be deduced through auto . Explicitly giving the type through -> std::array<event, l()> seems to fail (the first instance). Why is this? template <typename Lambda_T> constexpr static auto foo(Lambda_T l) -> std::array<event, l()> { return {}; } // error template <typename Lambda_T> constexpr static auto foo(Lambda_T l) { return std::array<event, (l())>{}; } // OK template