constexpr

C++11 enum with class members and constexpr link-time optimization

梦想与她 提交于 2019-12-18 08:34:13
问题 In my project I have a lot of enumerations that need to have additional attributes associated with the enumeration members and auxiliary static methods associated with the enumeration type. As much as I know, this is not possible to have with the standard enum class MyItem {...}, so for each enum class in my project I have an auxiliary class MyItemEnum that encapsulates these auxiliary static methods and also instantiates auxiliary instances of itself, so that I can access their methods in

Defining constexpr static data members

99封情书 提交于 2019-12-18 08:32:26
问题 So, I'm aware that in C++ static members can be initialized inside the class if they are a const literal type like the following class test{ public: static constexpr int stc = 1; private: int a = 0; int b = 0; int c = 0; }; and the static constexpr variable stc can be used where the compiler can directly substitute the value of the member i.e int main () {int array[test::stc];} However, if used in a context where the value cannot be directly substituted by the compiler: int main() { const int

C++11 constexpr function compiler error with ternary conditional operator (?:)

大兔子大兔子 提交于 2019-12-18 06:59:41
问题 What is wrong with this piece of code? #include <iostream> template<unsigned int N, unsigned int P=0> constexpr unsigned int Log2() { return (N <= 1) ? P : Log2<N/2,P+1>(); } int main() { std::cout << "Log2(8) = " << Log2<8>() << std::endl; return 0; } When compiling with gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) , I get the following error: log2.cpp: In function ‘constexpr unsigned int Log2() [with unsigned int N = 0u, unsigned int P = 1023u]’: log2.cpp:5:38: error: template

Simple variadic template function can't instantinate

£可爱£侵袭症+ 提交于 2019-12-18 06:59:24
问题 I'm aware that sizeof...(Args...) yields the number of types in a C++0x packed template argument list, but I wanted to implement it in terms of other features for demonstation purposes, but it won't compile. // This is not a solution -- overload ambiguity. // template <typename... Args> size_t num_args (); // Line 7 // template <> constexpr size_t num_args () { return 0; } template <typename H, typename... T> constexpr size_t num_args () // Line 16 { return 1 + num_args <T...> (); // *HERE* }

Initializing static constexpr variables and classes inside a struct

时光怂恿深爱的人放手 提交于 2019-12-18 05:47:07
问题 Here is my working code example: #include <iostream> template<typename B> class b { public: int y; constexpr b(int x) : y(x) { } constexpr void sayhi() { std::cout << "hi" << std::endl; } }; template<int x> struct A { static constexpr b<int> bee = x; static constexpr int y = x; // this one is fine and usable already, I don't have to do something like what I did on member bee inline static void sayhi() { std::cout << y << std::endl; } }; template<int x> constexpr b<int> A<x>::bee; // why do I

std::max() and std::min() not constexpr

故事扮演 提交于 2019-12-18 05:27:07
问题 I just noticed that the new standard defines min(a,b) and max(a,b) without constexpr . Examples from 25.4.7, [alg.min.max]: template<class T> const T& min(const T& a, const T& b); template<class T> T min(initializer_list<T> t); Isn't this a pity? I would have liked to write char data[ max(sizeof(A),sizeof(B)) ]; instead of char data[ sizeof(A) > sizeof(B) ? sizeof(A) : sizeof(B) ]; char data[ MAX(sizeof(A),sizeof(B)) ]; // using a macro Any reason why those can not be constexpr ? 回答1:

c++11 fast constexpr integer powers

浪子不回头ぞ 提交于 2019-12-18 04:43:12
问题 Beating the dead horse here. A typical (and fast) way of doing integer powers in C is this classic: int64_t ipow(int64_t base, int exp){ int64_t result = 1; while(exp){ if(exp & 1) result *= base; exp >>= 1; base *= base; } return result; } However I needed a compile time integer power so I went ahead and made a recursive implementation using constexpr: constexpr int64_t ipow_(int base, int exp){ return exp > 1 ? ipow_(base, (exp>>1) + (exp&1)) * ipow_(base, exp>>1) : base; } constexpr int64

template instantiation with constexpr function failure

孤街醉人 提交于 2019-12-18 03:14:35
问题 I have template class C that has a non-type but reference template parameter to a type P : class P { public: int x; int y; }; template <const P &x> class C { public: const int &f() { return x.x; } }; I declared a global variable of type P : P p = {33,44}; I also declared a function that returns a reference to p : constexpr const P &h() { return p; } And then tried to use these in the following : C<p> o; // line 1 C<h()> oo; // line 2 Of course I have no problem with the first instantiation

constexpr to concatenate two or more char strings

早过忘川 提交于 2019-12-17 23:47:44
问题 I'm trying to make a constexpr function that will concatenate an arbitrary number of char arrays by working from the following answer by Xeo, which concatenates two char arrays. https://stackoverflow.com/a/13294458/1128289 #include <array> template<unsigned... Is> struct seq{}; template<unsigned N, unsigned... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<unsigned... Is> struct gen_seq<0, Is...> : seq<Is...>{}; template<unsigned N1, unsigned... I1, unsigned N2, unsigned... I2>

constexpr to concatenate two or more char strings

久未见 提交于 2019-12-17 23:47:28
问题 I'm trying to make a constexpr function that will concatenate an arbitrary number of char arrays by working from the following answer by Xeo, which concatenates two char arrays. https://stackoverflow.com/a/13294458/1128289 #include <array> template<unsigned... Is> struct seq{}; template<unsigned N, unsigned... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<unsigned... Is> struct gen_seq<0, Is...> : seq<Is...>{}; template<unsigned N1, unsigned... I1, unsigned N2, unsigned... I2>