什么意思是“服从ODR"?在内联和constexpr函数的情况下? [英] What means "obey ODR" in case of inline and constexpr function?

查看:133
本文介绍了什么意思是“服从ODR"?在内联和constexpr函数的情况下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚读到constexpr和内联函数遵循一定义规则,但是它们的定义必须相同.所以我尝试一下:

I just read that constexpr and inline functions obey one-definition rule, but they definition must be identical. So I try it:

inline void foo() {
    return;
}

inline void foo() {
    return;
}

int main() {
    foo();
};

错误:重新定义了"void foo()",

error: redefinition of 'void foo()',
and

constexpr int foo() {
    return 1;
}

constexpr int foo() {
    return 1;
}

int main() {
    constexpr x = foo();
}; 

错误:'constexpr int foo()'的重新定义

error: redefinition of 'constexpr int foo()'

那么constexpr和内联函数可以服从ODR到底是什么意思?

So what exactly means that, constexpr and inline function can obey ODR?

推荐答案

我刚刚读到constexpr和内联函数遵循一定义规则,但是它们的定义必须相同.

I just read that constexpr and inline functions obey one-definition rule, but they definition must be identical.

这是指不同翻译单元中的内联函数.在您的示例中,它们都在同一个翻译单元中.

This is in reference to inline functions in different translations units. In your example they are both in the same translation unit.

草稿C ++标准对此进行了介绍 3.2一个定义规则 [basic.def.odr] 如下:

This is covered in the draft C++ standard 3.2 One definition rule [basic.def.odr] which says:

一个类类型(第9条),枚举类型(7.2)和内联函数的定义可以不止一个 外部链接(7.1.2),类模板(第14章),非静态功能模板(14.5.6),静态数据成员 类模板(14.5.1.3),类模板的成员函数(14.5.1.1)或模板专用化 程序中未指定哪些模板参数(14.7、14.5.5),前提是每个定义 出现在不同的翻译单元中,并且提供的定义满足以下要求.给定 这样的名为D的实体在多个翻译单元中定义,然后

There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then

并包括以下项目符号:

  • D的每个定义应由相同的令牌序列组成;和
  • each definition of D shall consist of the same sequence of tokens; and

这篇关于什么意思是“服从ODR"?在内联和constexpr函数的情况下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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