constexpr

Integral constant passed by value, treated as constexpr?

好久不见. 提交于 2019-12-20 17:38:51
问题 Although I've used code like this before, and it's clear that the compiler has enough information to work, I don't really understand why this compiles: template <class T, class I> auto foo(const T& t, I i) { return std::get<i>(t); } int main() { std::cerr << foo(std::make_tuple(3,4), std::integral_constant<std::size_t, 0>{}); return 0; } Live example: http://coliru.stacked-crooked.com/a/fc9cc6b954912bc5. Seems to work with both gcc and clang. The thing is that while integral_constant has a

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

杀马特。学长 韩版系。学妹 提交于 2019-12-20 11:53:19
问题 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) & !

Template and constexpr deduction at compiletime dependent on compiler and optimization flags

时光怂恿深爱的人放手 提交于 2019-12-20 07:26:14
问题 The following question is condensed from a much larger code. Therefore some expressions seem to be an overkill or unnecessary, but are crucial to the original code. Consider having a struct, which contains compile time constants and a simple container class: template<typename T> struct CONST { static constexpr T ONE() { return static_cast<T>( 1 ); } }; template<typename T> class Container { public: using value_type = T; T value; }; Now having a template function, that has a "specialization"

Comparing constexpr function parameter in constexpr-if condition causes error

余生长醉 提交于 2019-12-20 05:13:50
问题 I'm trying to compare a function parameter inside a constexpr-if statement. Here is a simple example: constexpr bool test_int(const int i) { if constexpr(i == 5) { return true; } else { return false; } } However, when I compile this with GCC 7 with the following flags: g++-7 -std=c++1z test.cpp -o test I get the following error message: test.cpp: In function 'constexpr bool test_int(int)': test.cpp:3:21: error: 'i' is not a constant expression if constexpr(i == 5) { return true; } However, if

constexpr and RTTI

孤人 提交于 2019-12-20 02:49:09
问题 I'd like to do something like this: template <typename T> constexpr ::std::size_t type_name_hash() { return ::std::hash<::std::string>()(typeid(T).name()); } Now, I know neither hash nor string are constexpr , but this could be worked around, assume they are constexpr . What I want to ask is, if RTTI was turned on, should a constexpr function computing a hash of typeid(T).name() still produce a compile-time constant? How about when RTTI is turned off? 回答1: What part of Run-Time Type

How to force const propagation through an inline function?

不羁岁月 提交于 2019-12-20 02:35:39
问题 I'm trying to coerce the pre-processor to perform some math for me so a constant gets propagated into inline assembly. Here's the reduced case: inline unsigned int RotateRight(unsigned char value, unsigned int amount) { COMPILE_ASSERT(((unsigned char)(amount%32)) < 32); __asm__ ("rorb %1, %0" : "+mq" (value) : "I" ((unsigned char)(amount%32))); return value; } The code above relies upon CPU specific functionality, and I'm OK with it (its actually a template specialization on x86/x64 Linux

How to force const propagation through an inline function?

為{幸葍}努か 提交于 2019-12-20 02:35:07
问题 I'm trying to coerce the pre-processor to perform some math for me so a constant gets propagated into inline assembly. Here's the reduced case: inline unsigned int RotateRight(unsigned char value, unsigned int amount) { COMPILE_ASSERT(((unsigned char)(amount%32)) < 32); __asm__ ("rorb %1, %0" : "+mq" (value) : "I" ((unsigned char)(amount%32))); return value; } The code above relies upon CPU specific functionality, and I'm OK with it (its actually a template specialization on x86/x64 Linux

Ill-Formed, No Diagnostic Required (NDR): ConstExpr Function Throw in C++14

心已入冬 提交于 2019-12-20 02:28:25
问题 #include <iostream> using namespace std; constexpr int f(bool b){ return b ? throw 0 : 0; } // OK constexpr int f() { return f(true); } // Ill-Formed, No Diagnostic Required int main(){ try{ f(); }catch( int x ){ cout << "x = " << x << endl; } return 0; } This code is an example from the C++14 Standard (ISO/IEC 14882:2014), Section 7.1.5, Paragraph 5: For a non-template, non-defaulted constexpr function or a non-template, non-defaulted, non-inheriting constexpr constructor, if no argument

Static templated constexpr nested class member

☆樱花仙子☆ 提交于 2019-12-20 02:15:47
问题 I have the following sample class Foo with nested class Bar and everything is constexpr : class Foo { private: template <typename T> struct Bar { constexpr Bar(){} constexpr int DoTheThing() const { return 1; } }; public: constexpr static auto b = Bar<int>{}; constexpr Foo() {} constexpr int DoTheThing() const { return b.DoTheThing(); } }; And I want to test that calling Foo::DoTheThing returns 1: int main() { constexpr Foo f; static_assert(f.DoTheThing() == 1, "DoTheThing() should return 1")

why is a const array not accessible from a constexpr function?

拥有回忆 提交于 2019-12-20 01:58:09
问题 i have a constexpr function named access, and i want to access one element from an array: char const*const foo="foo"; char const*const bar[10]={"bar"}; constexpr int access(char const* c) { return (foo == c); } // this is working constexpr int access(char const* c) { return (bar[0] == c); } // this isn't int access(char const* c) { return (bar[0] == c); } // this is also working i get the error: error: the value of 'al' is not usable in a constant expression why can't i access one of the