7

Title says it all and both of the usual ways do not work. What am I missing?

1.

class Cl { static constexpr double PI; }; constexpr double Cl::PI = 3.14; 

(26): error C2737: 'private: static double const Cl::PI' : 'constexpr' object must be initialized

2.

class Cl { static constexpr double PI = 3.14; }; 

(26): error C2864: 'Cl::PI' : a static data member with an in-class initializer must have non-volatile const integral type
type is 'const double'

In both attempts, the error is on the same line inside the class. I am using the VisualStudio/MSVC Nov 2013 CTP compiler.

Note that making the variable const is not a solution because I want to use this constant in both constexpr functions and normal functions.

5
  • 2. should work. But drop the spurious :. Commented May 4, 2014 at 9:18
  • @juanchopanza That was a copy/paste error. In my code, there is a superclass after the :. Commented May 4, 2014 at 9:20
  • Anyway, 2. should work. Commented May 4, 2014 at 9:54
  • 1
    Somewhat off topic, but M_PI is already defined in <cmath>. Commented May 4, 2014 at 15:00
  • @Edward Yup, PI is just an example. Commented May 4, 2014 at 22:48

2 Answers 2

6

By the tables and explanation from Stephan T. L. in this blog, the constexpr is indeed only partially implemented in VS Nov 2013 CTP.

The CTP supports C++11 constexpr, except for member functions. (Another limitation is that arrays aren't supported.) Also, it doesn't support C++14's extended constexpr rules.

(wish to put it in comments, but no sufficient points yet)

Edit: Just to add, in Herb's blog, there is near same question about static members, but the reply is the same as Stephan.

I think it is safe to simple say that Nov 2013 CTP not implement the required OP feature (send a bug report?) and wait for a Jul 2014 CTP or VS Next (sadly).

Sign up to request clarification or add additional context in comments.

2 Comments

+1 good answer (ironically, the use case from the OP isn't exactly covered by the exceptions, really, but hey, we can't expect an exhaustive list of broken features in the MSVC compiler! (Save trees)
Agree to @sehe comment, we don't have all the dos and don'ts of what was really working, so we are on a gray zone here (without a proper answer).
1

You can't really "initialize" a constexpr. As the keyword implies, it's a constant expression, not a variable.

It seems you just want to use const here.

The compiler in the second example just points out that you can't make all types const-expr.

Update: This appears to be a MSVC limitation.

are happy to oblige.

Indeed, the C++11 support page mentions: no constexpr support in MSVC2010-2013

11 Comments

Sure, but 3.14 is a constant variable, right? I want to use it in a constexpr function.
^ see edit. It's a literal, alright, but it's not a non-volatile const integral type, as required.
Maybe. This isn't really an answer, though :-/
How do you know it is a bug?
I'm using the Nov2013 CTP which supposedly supports constexpr. Anyway, I guess they only implemented it in some cases.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.