简体   繁体   English

可以在编译单元之间区分内联成员函数是否会破坏二进制兼容性?

[英]Can inline member functions differing across compilation units break binary compatibility?

I have a lot of code in a big project that has two general types of code, some that is done in a nice C++ style and has been code reviewed by an expert in C++ and some that is not and has not. 我在一个大型项目中有很多代码,它有两种通用类型的代码,其中一些代码以一种漂亮的C ++风格完成,并且由C ++专家进行了代码审查,有些则没有,也没有。 The code that is not has lots of for loops and unchecked array (heap) accesses consisting of both reads and writes. 代码中没有很多for循环和未经检查的数组(堆)访问,包括读取和写入。 Fortunately, all these accesses to the heap are done through a class. 幸运的是,所有这些对堆的访问都是通过类完成的。 For argument's sake, let's call it CArray . 为了论证,我们称之为CArray

CArray is defined entirely inside header files since it's templated. CArray完全在头文件中定义,因为它是模板化的。 It is roughly defined as follows (only showing relevant portions): 它大致定义如下(仅显示相关部分):

template <typename elementType> class CArray
{
    /// Most of class details I'm leaving out

public:
    inline elementType & operator[](unsigned int i)
    {
#ifdef CARRAY_BOUNDARY_DEBUGGING
        if(i >= m_numElements)
        {
            throw SomeException("CArray::operator[] going out of bounds");
        }
#endif
        return m_pArray[i];
    }

    /// also have corresponding const version of this operator[]

private:
    elementType *m_pArray;
    int m_numElements;
}

(Please assume that the constructor, destructor, copy constructor, assignment operator and the corresponding rvalue reference versions are done right. Basically assume that the user of the class everything they need to use this class properly. (请假设构造函数,析构函数,复制构造函数,赋值运算符和相应的右值参考版本都是正确的。基本上假设类的用户需要正确使用此类所需的一切。

Now the question is, if I have some compilation units ( .cpp files) that define CARRAY_BOUNDARY_DEBUGGING and then include this class (the ones needing code improvement/review), but others that do not (the ones that are rock solid): 现在的问题是,如果我有一些定义CARRAY_BOUNDARY_DEBUGGING编译单元( .cpp文件),然后包含这个类(需要代码改进/审查的那些),而其他那些没有(那些坚如磐石的):

  1. Is this guaranteed to be OK and not introduce problems since the class if often passed via copy, reference, and rvalue refrence (C++11 only) over compilation unit boundaries by C++03? 这是否保证是正常的并且不会引入问题,因为类经常通过C ++ 03通过复制,引用和右值引用(仅限C ++ 11)通过编译单元边界传递? C++11? C ++ 11?

  2. Is there a better way to try to do this? 有没有更好的方法来尝试这样做?

Edit: clarification. 编辑:澄清。 I know that non- inline functions must obey the one definition rule as stated in section 3.2 of the C++03 standard, but this is inline. 我知道非inline函数必须遵守C ++ 03标准第3.2节中所述的一个定义规则,但这是内联的。 Does the ODR still apply or is something else in effect here? ODR是否仍然适用或在此处有效? Also the C++03 standard states "An inline function shall be defined in every translation unit in which it is used." 此外,C ++ 03标准规定“内联函数应在每个使用它的翻译单元中定义。”

No, it's not okay, it's UB. 不,这不好,这是UB。 Inlined definitions must be the same across the entire program. 整个程序中的内联定义必须相同。 If you want to have additional logic used in some places, but not all, just make it another member (also writing CArray is silly): 如果你想在某些地方使用额外的逻辑,但不是全部,只要让它成为另一个成员(也写CArray是愚蠢的):

struct silly_array {
    T& operator[](int x) { /* unchecked */ }
    T& at(int x) { /* check bounds */ return (*this)[x]; }
};

You forgot to read the rest of the sentence you quoted. 你忘了阅读你所引用的句子的其余部分。 7.1.2(4) "An inline function shall be defined in every translation unit in which it is used and shall have exactly the same definition in every case (3.2)." 7.1.2(4)“内联功能应在每个使用它的翻译单元中定义, 并且在每种情况下都应具有完全相同的定义 (3.2)。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM