constexpr

Calling constexpr function for bitset template parameter

人走茶凉 提交于 2019-12-25 06:47:56
问题 I'm trying to type alias the std::bitset class where the template parameter N is calculated using a constexpr function. However, this approach seems to be running into a wall. The code currently looks like this: static constexpr std::size_t ComponentCount() noexcept { return 3U; } static constexpr std::size_t TagCount() noexcept { return 5U; } using Bitset = std::bitset<ComponentCount() + TagCount()>; And the error I'm receiving is as follows: 1>error C2975: '_Bits': invalid template argument

How to implement std::optional's copy constructor?

一曲冷凌霜 提交于 2019-12-25 02:33:11
问题 I am implementing std::optional, but have run into a snag with one of its copy constructors. Here is a sketch of my implementation: #include <type_traits> template<typename T> class optional { public: constexpr optional() : m_is_engaged(false) {} constexpr optional(const optional &other) : m_is_engaged(false) { operator=(other); } constexpr optional &operator=(const optional &other) { if(other.m_is_engaged) { return operator=(*other); } else if(m_is_engaged) { // destroy the contained object

Compiler error in uninstantiated template when a constexpr function calls a non-constexpr one

冷暖自知 提交于 2019-12-24 15:03:09
问题 In the following piece of C++11 code, the function get is constexpr but it tries to construct an instance via the non- constexpr constructor. template <typename T> struct S { S() {} static constexpr S get() { return S(); } }; int main() { // S<int> s1; // auto s2 = s1.get(); } While this code compiles with GCC, it fails with the compiler we use at work with the message constexpr function return is non-constant. We started a discussion whether the compiler is allowed to issue an error in this

Why can't a static constexpr member variable be passed to a function?

南笙酒味 提交于 2019-12-24 05:58:30
问题 The following code produces an undefined reference to 'Test::color' . #include <iostream> struct Color{ int r,g,b; }; void printColor(Color color) { //printing color } class Test { static constexpr Color color = {242,34,4}; public: void print(){ printColor(color); } }; int main() { Test test; test.print(); return 0; } Why does this code produce the above error and what is the best way to avoid it, considering I want to use the latest version of the standard, C++17? Should I define the static

How to get the compiler to warn that this is an invalid bool?

拜拜、爱过 提交于 2019-12-24 03:51:40
问题 We just got burnt by a typo: " constexpr bool maxDistance=10000; " Both gcc and clang compile this with no warning. The real error here is that the variable shouldn't have been of type bool, but should have been an integer type instead. How can we ensure we get a compiler warning in future? #include <iostream> constexpr bool number = 1234; int main(int argc, char* argv[]) { std::cout << number + 10000 << std::endl; // prints 10001. return number; } The error here is that the variable is

C++ static constexpr member redeclaration outside of class

那年仲夏 提交于 2019-12-24 02:58:08
问题 For the following code, why does the first case in main work fine without the redeclaration of Foo::bar, whereas the second case with the function requires it? struct Foo{ static constexpr int bar = 30; }; //Declaration of Foo::bar outside of struct constexpr int Foo::bar; int returnconstexpr(const int& x) { return x; } int main() { //Ok without declaration outside of struct std::cout << Foo::bar << std::endl; //Requires declaration outside of struct std::cout << returnconstexpr(Foo::bar) <<

what is the difference between “constexpr” and “static constexpr” variables of the non-class type in a header file?

牧云@^-^@ 提交于 2019-12-24 01:53:04
问题 I have a header file test.hxx which will be included in multiple translation units. The header file is as below: namespace program_exec { static constexpr int DEFAULT_VAL = 0; static constexpr char *name = "proc_exec"; } I have included this header file in multiple translation units (*.cxx) and it works fine. But removing the static infront of constexpr char* causes a linking error i.e, if I change the static constexpr char *name = "proc_exec" to constexpr char *name = "proc_exec"; I am

literal string declared static in constexpr function

懵懂的女人 提交于 2019-12-24 01:44:46
问题 I'm trying to make constexpr some existing code, but getting message error: 'my_string' declared 'static' in 'constexpr' function Much simplified, the code is: template <typename T> constexpr int foo(const int x) { static // error: 'my_string' declared 'static' in 'constexpr' function constexpr char my_string[] = "my foo error message!"; if (x == 0) { std::cout << my_string << std::endl; } return x; } class boo { public: constexpr boo() { static // error: 'constructor_string' declared 'static

Switch case statement with member variable in case

*爱你&永不变心* 提交于 2019-12-24 00:24:58
问题 I am trying to find a way to evaluate a switch - case statement using a member variable in the case part. I thought that having a global static variable like below would be allowed as const-expression. Unfortunately the compiler tells me the opposite: error: ‘ll’ cannot appear in a constant-expression error: ‘.’ cannot appear in a constant-expression Is there any keyword or anything that would allow this idea to work? I am not an expert of C++11, and I heard about constexpr . enum MyEnum2 {A

Undefined behavior when constexpr-evaluating negative bitshift?

吃可爱长大的小学妹 提交于 2019-12-23 20:51:11
问题 Consider the following snippet of code: int main(){ constexpr int x = -1; if(x >= 0){ constexpr int y = 1<<x; } } GCC 7 (and probably other versions of GCC) refuses to compile this and says: error: right operand of shift expression '(1 << -1)' is negative [-fpermissive] I can guess where this may have come from: the constexpr declaration on y makes GCC evaluate y at compile time, where it might be negative. Removing the constexpr fixes the error. However, is this undefined behavior by the