Should I use constexpr?

Still learning a bit about constexpr and it's best uses, so pardon my ignorance. I have a few "getter" functions with a single return value, would this be a good candidate for a constepr?

 
  constexpr unsigned int getSpeed() const { return m_speed; }


Is this sort of implementation really something that needs to be done at compile time? If this isn't a good function to use constexpr on then what would be a good real world example of when to use it?
Last edited on
I think one needs to consider the whole class. Do you want to be able to use the class in a constexpr context? For constexpr to make sense for non-static member functions you need to have at least one constexpr constructor.
Last edited on
The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time. Such variables and functions can then be used where only compile time constant expressions are allowed (provided that appropriate function arguments are given). A constexpr specifier used in an object declaration or non-static member function (until C++14) implies const. A constexpr specifier used in a function or static member variable (since C++17) declaration implies inline.

taken from https://en.cppreference.com/w/cpp/language/constexpr

I suppose it make sense, since usually getters should probably be inlined, and constexpr in function implies inline ( not sure if that since C++17 refers to function and static member var, or just static member var, but since it is going to be in, seems like a good idea )
Topic archived. No new replies allowed.