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


i have class dimension defined (like classes) in file dimension.h:

class dimension { public:      constexpr dimension() noexcept;      constexpr dimension(int w, int h) noexcept;      int width;     int height;  }; 

i thought could, in classes, put definition in separate dimension.cpp:

#include "dimension.h"  constexpr dimension::dimension() noexcept : width(0), height(0) {}  constexpr dimension::dimension(int w, int h) noexcept : width(w), height(h) {} 

but when try use class, compiler tells me:

warning: inline function 'constexpr dimension::dimension()' used never defined

and while linking:

undefined reference 'pong::graphics::dimension::dimension()'

(same other constructor)

if define class in header so:

class dimension { public:      constexpr dimension() noexcept : width(0), height(0) {}      constexpr dimension(int w, int h) noexcept : width(w), height(h) {}      int width;     int height;  }; 

and omit .cpp file, works fine.

i'm using gcc 4.9.2. why separate definition not work?

if constexpr function not defined inside header, compiler can not see definition of constexpr functions while compiling other source files.

obviously, if can't see definition of functions, can't perform steps necessary calculate them @ compile-time. constexpr functions must defined everywhere used.

thanks @igortandetnik:
[dcl.constexpr] §7.1.5/2

constexpr functions , constexpr constructors implicitly inline.

[basic.def.odr] §3.2/4

an inline function shall defined in every translation unit in odr-used.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -