简体   繁体   中英

Is there a way to declare a public static const that will be defined in the source file using constexpr (and what difference is there)?

In my header I have a public static const declared and I define in the source file as a class member. I want to define it in the source file because I am including and using a constant from an it, and I don't want to include in my header.

If I use static constexpr in the header it requires a definition there.

header

public:
  static const double DEG_TO_RADIANS;

source

#include <math.h>

const double MyClass::DEG_TO_RADIANS = (M_PI/180.0);

Is this a situation where you just can't use constexpr, does this matter at all?

Why does a static const allow me to not define the variable but static constexpr doesn't?

Edit: The M_PI is a simple example of this situation.

The whole point of constexpr is to use it for values that can be known at compile time. That doesn't work if the definition is in a completely different file.

Static const requires you to provide a definition which a linker can connect between different files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM