constexpr

constexpr and bizzare error

蓝咒 提交于 2019-12-23 19:17:08
问题 I'm having: constexpr bool is_concurrency_selected()const { return ConcurrentGBx->isChecked();//GBx is a groupbox with checkbox } and I'm getting error: C:\...\Options_Dialog.hpp:129: error: enclosing class of 'bool Options_Dialog::is_concurrency_selected() const' is not a literal type Any thoughts on why? 回答1: It means your class is not a literal type... This program is invalid, because Options is not a literal class type. But Checker is a literal type. struct Checker { constexpr bool

“integer constant overflow” warning in constexpr

Deadly 提交于 2019-12-23 13:34:29
问题 I'm trying to find a constexpr compatible hash function to use for hashing strings in compile-time. The number of strings are really small (<10) and I have a separate check for collisions, so the algorithm can be far from perfect. I found the following version of FNV1A somewhere on the internet: static constexpr unsigned int Fnv1aBasis = 0x811C9DC5; static constexpr unsigned int Fnv1aPrime = 0x01000193; constexpr unsigned int hashFnv1a(const char *s, unsigned int h = Fnv1aBasis) { return !*s

Constexpr member function in class template

孤者浪人 提交于 2019-12-23 12:24:29
问题 The following code fails to compile: // template<class> struct S { int g() const { return 0; } constexpr int f() const { return g(); } }; int main() { S /*<int>*/ s; auto z = s.f(); } GCC, for example, complains: error: call to non-constexpr function ‘int S::g() const’ . This is perfectly reasonable. But if I turn S into a template, the code compiles (checked with MSVC 15.3, GCC 7.1.0, clang 4.0.1). Why? Does constexpr has any special meaning in class templates? As far as I understand it,

Why does a constexpr function behave differently for a reference?

我的未来我决定 提交于 2019-12-23 12:06:32
问题 Inspired by Counting function arguments at compile time Consider this code: template <typename... Args> constexpr int count(Args&&...) { return sizeof...(Args); } void foo(int value) { static_assert(count(value) >= 0); // OK const int& ref = 7; static_assert(count(ref) >= 0); // Error } First static_assert works fine. Second gives an error: <source>:12:19: error: static_assert expression is not an integral constant expression static_assert(count(ref) >= 0); ^~~~~~~~~~~~~~~ <source>:12:25:

Is the “static initialization order fiasco” a concern for constexpr variables?

老子叫甜甜 提交于 2019-12-23 10:54:38
问题 If I initialize a constexpr variable foo with a non-default value in one translation unit and then a initialize another constexpr variable bar with foo in another translation unit is it possible for bar to be initialized before foo resulting in a bar that was initialized by a zero-or-default-initialized foo . i.e. Unlike in the non-constexpr case (where the static initialization order fiasco is in effect) will the compiler and linker analyze dependency ordering to guarantee the correct result

Non-type template argument is not a constant expression

假如想象 提交于 2019-12-23 10:15:08
问题 I have the following code: #include <cstdlib> #include <cstdio> #include <atomic> enum ATYPE { Undefined = 0, typeA, typeB, typeC }; template<ATYPE TYPE = Undefined> struct Object { Object() { counter++; } static std::atomic<int> counter; }; template<ATYPE TYPE> std::atomic<int> Object<TYPE>::counter(1); template<ATYPE TYPE> void test() { printf("in test\n"); Object<TYPE> o; } int main(int argc, char **argv) { test<typeA>(); printf("%d\n", Object<typeA>::counter.load()); Object<typeA>:

Error with constexpr(gcc) - error: a brace-enclosed initializer is not allowed here before '{' token

寵の児 提交于 2019-12-23 09:58:02
问题 struct X { constexpr static char a1[] = "hello"; // Okay constexpr static const char* a2[] = {"hello"}; // Error }; int main(){} Compiling with gcc gives the error: error: a brace-enclosed initializer is not allowed here before '{' token Is this an illegal use of constexpr? EDIT I tried 3 different versions of gcc, and it compiled on the newest 4.7.0 I have (I just downloaded it, I'm using mingw-w64), so it looks to be a fixed bug (a link to the bug would be nice though!). 4.7.0 20120311

Having a constexpr static string gives a linker error

混江龙づ霸主 提交于 2019-12-23 09:52:06
问题 The following program gives me a link-time error: #include <iostream> struct Test { static constexpr char text[] = "Text"; }; int main() { std::cout << Test::text << std::endl; // error: undefined reference to `Test::text' } The error message is /tmp/main-35f287.o: In function `main': main.cpp:(.text+0x4): undefined reference to `Test::text' main.cpp:(.text+0x13): undefined reference to `Test::text' clang: error: linker command failed with exit code 1 (use -v to see invocation) Ok. Let's try

Can arrays be indexed at compile time?

∥☆過路亽.° 提交于 2019-12-23 09:49:41
问题 In this comment to another question, the user hvd stated the following: ... although string literals can be passed to constexpr functions, and array indexing is allowed on string literals in constant expressions, an indexing operation on a constexpr function parameter doesn't qualify as a constant expression. I didn't fully understand what was meant. Does it mean that the hash_value variable in the following code #include <cstddef> // Compute the hash of a string literal adding the values of

ADL does not work in constexpr functions (clang only)

我与影子孤独终老i 提交于 2019-12-23 09:20:03
问题 The following code compiles with MSVC and gcc, but not with clang. Why is that so? It seems like if ADL would not work if CallFoo () is constexpr . See the comment. template <class T> constexpr void CallFoo () // Remove constexpr to fix clang compilation error. { Foo (T ()); } class Apple {}; int main () { CallFoo<Apple> (); } constexpr void Foo (Apple) { } Clang error message (see on godbolt.org): <source>:4:5: error: use of undeclared identifier 'Foo' Foo (T ()); ^ <source>:13:5: note: in