one-definition-rule

Why C++ linker is silent about ODR violation?

别来无恙 提交于 2019-12-23 21:04:28
问题 Let's consider some synthetic but expressive example. Suppose we have Header.h: Header1.h #include <iostream> // Define generic version template<typename T> inline void Foo() { std::cout << "Generic\n"; } Header2.h void Function1(); Header3.h void Function2(); Source1.cpp #include "Header1.h" #include "Header3.h" // Define specialization 1 template<> inline void Foo<int>() { std::cout << "Specialization 1\n"; } void Function1() { Foo<int>(); } Later I or some else defines similar conversion

undefined reference to class static constexpr struct, g++ vs clang

混江龙づ霸主 提交于 2019-12-23 10:25:36
问题 This is my code, a.cpp struct int2 { int x, y; }; struct Foo{ static constexpr int bar1 = 1; static constexpr int2 bar2 = {1, 2}; }; int foo1(){ return Foo::bar1; // this is ok for both clang++ and g++ } int2 foo2(){ return Foo::bar2; // undefined reference to `Foo::bar2' in clang++ } int main(){ std::cout << foo2().x << std::endl; return 0; } use clang to compile, clang++ -std=c++11 a.cpp /tmp/a-0dba90.o: In function `foo2()': a.cpp:(.text+0x18): undefined reference to `Foo::bar2' clang-7:

undefined reference to class static constexpr struct, g++ vs clang

蹲街弑〆低调 提交于 2019-12-23 10:24:08
问题 This is my code, a.cpp struct int2 { int x, y; }; struct Foo{ static constexpr int bar1 = 1; static constexpr int2 bar2 = {1, 2}; }; int foo1(){ return Foo::bar1; // this is ok for both clang++ and g++ } int2 foo2(){ return Foo::bar2; // undefined reference to `Foo::bar2' in clang++ } int main(){ std::cout << foo2().x << std::endl; return 0; } use clang to compile, clang++ -std=c++11 a.cpp /tmp/a-0dba90.o: In function `foo2()': a.cpp:(.text+0x18): undefined reference to `Foo::bar2' clang-7:

I just can not understand DR 712

落爺英雄遲暮 提交于 2019-12-23 07:45:51
问题 DR 712 was responsible for the change in the wording of [basic.def.odr]/2 in C++11 to the current wording today, in [basic.def.odr]2 and 3. But I'm still trying to understand the reason for the change, as stated in the DR, as follows: 712. Are integer constant operands of a conditional-expression “used?” In describing static data members initialized inside the class definition, 9.2.3.2 [class.static.data] paragraph 3 says, The member shall still be defined in a namespace scope if it is used

Linker error with static constant that doesn't seem to be odr-used

旧时模样 提交于 2019-12-21 17:57:22
问题 The definition in the standard for odr-used is pretty confusing when you get into the details (at least, for me it is). I generally rely on the informal definition of "If a reference is taken", except when an lvalue-to-rvalue conversion is available. For integral constants, they should be treated as rvalues, which seems like it should be excluded from the reference rule. Here is my sample code that is failing to link: class Test { public: Test(); static constexpr int MIN_VALUE { 5 }; int m

C++: Different classes with the same name in different translation units

不问归期 提交于 2019-12-21 12:08:04
问题 Consider the following example: // usedclass1.hpp #include <iostream> class UsedClass { public: UsedClass() { } void doit() { std::cout << "UsedClass 1 (" << this << ") doit hit" << std::endl; } }; // usedclass2.hpp #include <iostream> class UsedClass { public: UsedClass() { } void doit() { std::cout << "UsedClass 2 (" << this << ") doit hit" << std::endl; } }; // object.hpp class Object { public: Object(); }; // object.cpp #include "object.hpp" #include "usedclass2.hpp" Object::Object() {

Is there a way to detect inline function ODR violations?

我只是一个虾纸丫 提交于 2019-12-21 07:14:16
问题 So I have this code in 2 separate translation units: // a.cpp #include <stdio.h> inline int func() { return 5; } int proxy(); int main() { printf("%d", func() + proxy()); } // b.cpp inline int func() { return 6; } int proxy() { return func(); } When compiled normally the result is 10 . When compiled with -O3 (inlining on) I get 11 . I have clearly done an ODR violation for func() . It showed up when I started merging sources of different dll's into fewer dll's. I have tried: GCC 5.1 -Wodr

passing a static constexpr variable by universal reference?

狂风中的少年 提交于 2019-12-19 19:54:57
问题 In the following, static constexpr member L is initialized in-class A and then passed by value or by (universal) reference. The latter fails in Clang but not in GCC, and behaviour is slightly different for member/non-member functions. In more detail: #include <iostream> using namespace std; struct A { static constexpr size_t L = 4; template <typename T> void member_ref(T&& x) { cout << std::forward<T>(x) << endl; } template <typename T> void member_val(T x) { cout << x << endl; } }; template

In class static const ODR

橙三吉。 提交于 2019-12-18 13:27:47
问题 I am a bit confused by the static in-class initialization of a const member. For example, in the code below: #include <iostream> struct Foo { const static int n = 42; }; // const int Foo::n; // No ODR void f(const int& param) { std::cout << param << std::endl; } int g(const int& param) { return param; } template<int N> void h() { std::cout << N << std::endl; } int main() { // f(Foo::n); // linker error, both g++/clang++ std::cout << g(Foo::n) << std::endl; // OK in g++ only with -O(1,2 or 3)

Inline constructors and One Definition Rule

纵饮孤独 提交于 2019-12-18 07:02:49
问题 Consider following source files 1.cpp #include <iostream> using namespace std; struct X { X() { cout << "1" << endl; } }; void bar(); void foo() { X x; } int main() { foo(); bar(); return 0; } 2.cpp #include <cstdio> struct X { X() { printf("2\n"); } }; void bar() { X x; } Is program compiled from these files well-formed? What should be in it's output? I've expected linker error due to violation of One Definition Rule or output "1 2". However it prints out "1 1" when compiled with g++ 3.4 and