是否可以在头中声明constexpr类并在单独的.cpp文件中定义它? [英] Is it possible to declare constexpr class in a header and define it in a seperate .cpp file?

查看:626
本文介绍了是否可以在头中声明constexpr类并在单独的.cpp文件中定义它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 Dimension ,它在文件Dimension.h中定义(像我所有的类一样):

  class Dimension 
{
public:

constexpr Dimension()noexcept;

constexpr Dimension(int w,int h)noexcept;

int width;
int height;

};

我想我可以像所有的类一样,把定义放在一个独立的Dimension.cpp:

  #includeDimension.h

constexpr Dimension :: Dimension()noexcept:width 0),height(0){}

constexpr Dimension :: Dimension(int w,int h)noexcept:width(w),height(h){}



但是当我尝试使用类时,编译器会告诉我:



warning:inline function' constexpr Dimension :: Dimension()'used but never defined



/ p>

未定义引用 pong :: graphics :: Dimension :: Dimension()'



(与其他构造函数相同)



如果我在头文件中定义类如下:

  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;

};

并省略.cpp文件,一切正常。



我使用GCC 4.9.2。为什么单独的定义不起作用?

解决方案

如果 constexpr 未在头文件中定义,编译器在编译所有其他源文件时无法看到 constexpr 函数的定义。



显然,如果他看不到函数的定义,他不能执行在编译时计算它们所需的步骤。因此,所有 constexpr 函数必须在它们使用的任何地方定义。



感谢@IgorTandetnik:

[dcl.constexpr]§7.1.5/ 2


constexpr 函数和 constexpr 构造函数隐式内联。


[basic.def.odr]§3.2/ 4


内联函数应在每个使用odr的翻译单元中定义。



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

class Dimension
{
public:

    constexpr Dimension() noexcept;

    constexpr Dimension(int w, int h) noexcept;

    int width;
    int height;

};

I thought I could, like in all my classes, put the definition in a seperate 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 I try to use the class, the compiler tells me:

warning: inline function 'constexpr Dimension::Dimension()' used but never defined

and while linking:

undefined reference to 'pong::graphics::Dimension::Dimension()'

(same with the other constructor)

If I define the class in the header like 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 the .cpp file, everything works fine.

I'm using GCC 4.9.2. Why does seperate definition not work?

解决方案

If a constexpr function is not defined inside the header, the compiler can not see the definition of the constexpr functions while compiling all the other source files.

Obviously, if he can't see the definition of the functions, he can't perform the steps necessary to calculate them at compile-time. Thus all constexpr functions must be defined everywhere they are used.

Thanks @IgorTandetnik:
[dcl.constexpr] §7.1.5/2

constexpr functions and constexpr constructors are implicitly inline.

[basic.def.odr] §3.2/4

An inline function shall be defined in every translation unit in which it is odr-used.

这篇关于是否可以在头中声明constexpr类并在单独的.cpp文件中定义它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆