Can using a lambda in header files violate the ODR?

后端 未结 2 2085
自闭症患者
自闭症患者 2020-12-02 12:18

Can the following be written in a header file:

inline void f () { std::function func = [] {}; }

or

class C {         


        
2条回答
  •  爱一瞬间的悲伤
    2020-12-02 12:23

    UPDATED

    After all I agree with @Columbo answer, but want to add the practical five cents :)

    Although the ODR violation sounds dangerous, it's not really a serious problem in this particular case. The lambda classes created in different TUs are equivalent except their typeids. So unless you have to cope with the typeid of a header-defined lambda (or a type depending on the lambda), you are safe.

    Now, when the ODR violation is reported as a bug, there is a big chance that it will be fixed in compilers that have the problem e.g. MSVC and probably some other ones which don't follow the Itanium ABI. Note that Itanium ABI conformant compilers (e.g. gcc and clang) are already producing ODR-correct code for header-defined lambdas.

提交回复
热议问题