Is it possible to declare constexpr class in a header and define it in a separate .cpp file?

前端 未结 2 944
梦毁少年i
梦毁少年i 2020-12-01 10:28

I have a class Dimension which I defined (like all my classes) in a file Dimension.h:

class Dimension
{
public:

    constexpr Dimension() noexc         


        
2条回答
  •  抹茶落季
    2020-12-01 10:45

    What you're asking can be accomplished, with a significant limitation: the constexpr function can then only be called from inside the translation unit (i.e. source file) where it is defined. This is not appropriate for the example you gave, since the constructor is meant to be part of the class' public interface. However it can be very useful in other cases, e.g. to define private methods as constexpr and then use their return values in expressions that need to be known at compile time, e.g. template instantiations, switch statement labels, etc.

提交回复
热议问题