one-definition-rule

One definition rule about class member access expression

拜拜、爱过 提交于 2019-12-10 14:14:12
问题 In N4296, 3.2 [basic.def.odr]p3: A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion to x yields a constant expression that does not invoke any non-trivial functions and, if x is an object, ex is an element of the set of potential results of an expression e , where either the lvalue-to-rvalue conversion is applied to e , or e is a discarded-value expression. How to explain this paragraph? I found two

Necessity of forward-declaring template functions

时光毁灭记忆、已成空白 提交于 2019-12-10 00:45:01
问题 I recently created this example code to illustrate C++11 variadic template function usage. template <typename Head, typename... Tail> void foo (Head, Tail...); template <typename... Tail> void foo (int, Tail...); void foo () {} template <typename... Tail> void foo (int x, Tail... tail) { std :: cout << "int:" << x; foo (tail...); } template <typename Head, typename... Tail> void foo (Head x, Tail... tail) { std :: cout << " ?:" << x; foo (tail...); } foo (int (123), float (123)); // Prints

About ODR-violations and template variables

青春壹個敷衍的年華 提交于 2019-12-09 16:39:31
问题 I know that template functions don't suffer of multiple definitions when linking, like member functions defined inside a class, which are inline by default. Also, constexpr objects have internal linkage, but template variables have external linkage (I mean at namespace scope and for C++14 in both cases). What about? template<class T> constexpr T i_am_odr_safe{}; Does i_am_odr_safe have external or internal linkage in C++14? and is it safe regarding multiple-definitions like function templates

Crash when destructors for static objects are executing

夙愿已清 提交于 2019-12-09 03:44:23
There's a subtle bug that wouldn't manifest predictably in a piece of our software. It happens when global destructors are executing. Often it's a "double-free" error, but I've seen other things as well: NULL-ptr dereference, ptr dereference to an address where nothing is allocated, misaligned access (because the pointer had a garbage value), issues related to a corrupt stack ... the list goes on. The cause of these mysterious & difficult to reproduce bugs: a subtle violation of the One Definition Rule. A little background ... Since I was a little tike this software was being linked using the

C++ - Define member function outside template-class but in header

不羁岁月 提交于 2019-12-09 03:23:16
问题 I have defined a simple class-template with one member function. It is defined outside the class with an additional (explicit) specialization, also defined outside the class. All in one headerfile. If you include this header in multiple translation units you get a linker error due to One-Definition-Rule. // Header with a template template <class T> class TestClass { public: TestClass() {}; ~TestClass() {}; bool MemberFunction(); }; template <class T> bool TestClass<T>::MemberFunction() {

using constants in header file with ODR compliance

拜拜、爱过 提交于 2019-12-09 01:02:25
问题 Looking at another question I realized that I can't use objects or functions from an anonymous namespace through a header file since it'll cause ODR violations in class definitions or inline functions. If this is the case, then is it possible to use named const or constexpr static objects in inline functions or in classes safely? For example, if CONSTANT was inside of namespace below it would be unsafe, but is it okay to use a constant with static linkage? // some header file to be included

ODR of template class with static constexpr member

本小妞迷上赌 提交于 2019-12-08 05:38:11
问题 I know, there are many answered question about linkage of a static (constexpr) members. But I wonder, why using a template class out-of-line definition works in a header file but not for a specialized class. a) This works without linker error: template<typename, typename> struct Foobar; template<typename T> struct Foobar<int, T> { static constexpr std::array<int, 1> a = {{1}}; }; template<typename T> constexpr std::array<int, 1> Foobar<int, T>::a; // foo.cpp std::cout << Foobar<int, int>::a[0

Crash when destructors for static objects are executing

自古美人都是妖i 提交于 2019-12-08 05:30:24
问题 There's a subtle bug that wouldn't manifest predictably in a piece of our software. It happens when global destructors are executing. Often it's a "double-free" error, but I've seen other things as well: NULL-ptr dereference, ptr dereference to an address where nothing is allocated, misaligned access (because the pointer had a garbage value), issues related to a corrupt stack ... the list goes on. 回答1: The cause of these mysterious & difficult to reproduce bugs: a subtle violation of the One

constexpr and ODR

被刻印的时光 ゝ 提交于 2019-12-07 04:29:38
问题 If we have a header file widget.hpp with the contents below: constexpr int foo = 10; struct widget { int bars[foo]; }; ...and we have two translation units generated from two source files which both only include widget.hpp , does this violate the one definition rule (more specifically, does the use of foo violate the one definition rule)? foo has internal linkage but it is also a constant expression. From my reading of 3.2.6 in the C++11 standard, which I will quote below, this is well-formed

How to avoid violating ODR with traits classes

给你一囗甜甜゛ 提交于 2019-12-06 00:51:41
问题 On reading code online from production libraries I found something like this Traits.hpp template <typename Type> class Traits { template <typename T, detail::EnableIfIsInstantiation<T, Type>* = nullptr> static void foo(T& object) { object.foo(); } }; SpecialTraits.hpp template <> class Traits<Special> { static void foo(Special& object) { object.foo(); } static void foo(Special&& object) { object.special_foo(); } }; This will cause an ODR violation if a library instantiates a type that uses