one-definition-rule

Non-overloadable non-inline function definitions in different translation units

落爺英雄遲暮 提交于 2020-06-27 11:35:10
问题 Let's say I have 2 TUs with 2 non-inline function definitions with external linkage which differ only in their return types. Which paragraph(s) my program violates? [basic.def.odr]/4 says: Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program outside of a discarded statement; no diagnostic required. But This paragraph says "that is odr-used" which may or may not be the case. How do I tell if I define the same non-inline

ASAN detects ODR violation of vtable of class which is shared with dynamically loaded library

天涯浪子 提交于 2020-06-11 12:46:10
问题 I'm working on a project which has a "util" library containing stuff like logging, assertion handling etc. This is compiled into a static library with -fPIC added. I also have a plugin system, where the plugins are shared libraries loaded at runtime via dlopen . Those plugins and the main executable both use the static util library. The problem: Now I'm getting AddressSanitizer: odr-violation errors when using ASAN. The issue is size=40 'vtable for StdStreamWriter' reported twice where

ASAN detects ODR violation of vtable of class which is shared with dynamically loaded library

|▌冷眼眸甩不掉的悲伤 提交于 2020-05-30 07:35:12
问题 I'm working on a project which has a "util" library containing stuff like logging, assertion handling etc. This is compiled into a static library with -fPIC added. I also have a plugin system, where the plugins are shared libraries loaded at runtime via dlopen . Those plugins and the main executable both use the static util library. The problem: Now I'm getting AddressSanitizer: odr-violation errors when using ASAN. The issue is size=40 'vtable for StdStreamWriter' reported twice where

Inlining Template Specialization

烂漫一生 提交于 2020-05-27 02:18:33
问题 If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template<typename T> void foo(const T param) { cout << param << endl; } But I get one definition rule (ODR) errors when I add a specalization to foo.h: template<> void foo(const bool param) { cout << param << endl; } Obviously I can solve this by inline 'ing the specialization. My question is, why do I need to? If the template doesn't violate ODR, why does a specialization? 回答1: An

Inlining Template Specialization

↘锁芯ラ 提交于 2020-05-27 02:16:00
问题 If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template<typename T> void foo(const T param) { cout << param << endl; } But I get one definition rule (ODR) errors when I add a specalization to foo.h: template<> void foo(const bool param) { cout << param << endl; } Obviously I can solve this by inline 'ing the specialization. My question is, why do I need to? If the template doesn't violate ODR, why does a specialization? 回答1: An

Inlining Template Specialization

◇◆丶佛笑我妖孽 提交于 2020-05-27 02:14:58
问题 If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template<typename T> void foo(const T param) { cout << param << endl; } But I get one definition rule (ODR) errors when I add a specalization to foo.h: template<> void foo(const bool param) { cout << param << endl; } Obviously I can solve this by inline 'ing the specialization. My question is, why do I need to? If the template doesn't violate ODR, why does a specialization? 回答1: An

Inlining Template Specialization

落爺英雄遲暮 提交于 2020-05-27 02:14:09
问题 If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template<typename T> void foo(const T param) { cout << param << endl; } But I get one definition rule (ODR) errors when I add a specalization to foo.h: template<> void foo(const bool param) { cout << param << endl; } Obviously I can solve this by inline 'ing the specialization. My question is, why do I need to? If the template doesn't violate ODR, why does a specialization? 回答1: An

What is the intention of ODR?

非 Y 不嫁゛ 提交于 2020-03-14 05:23:15
问题 I do understand what ODR says, but I don't understand what it tries to achieve. I see two consequences of violating it - user will get syntax error, which is totally fine. And also there may be some fatal errors, and again the user would be the only one who's guilty. As example of violating ODR and getting some fatal error I imagine like this: a.cpp struct A { int a; double b; }; void f(A a) { std::cout << a.a << " " << a.b << std::endl; } main.cpp struct A { int a; int b; }; void f(A a); int

What is the intention of ODR?

泄露秘密 提交于 2020-03-14 05:20:06
问题 I do understand what ODR says, but I don't understand what it tries to achieve. I see two consequences of violating it - user will get syntax error, which is totally fine. And also there may be some fatal errors, and again the user would be the only one who's guilty. As example of violating ODR and getting some fatal error I imagine like this: a.cpp struct A { int a; double b; }; void f(A a) { std::cout << a.a << " " << a.b << std::endl; } main.cpp struct A { int a; int b; }; void f(A a); int

How would use of unnamed namespaces in headers cause ODR-violations?

那年仲夏 提交于 2020-01-31 11:47:20
问题 In the Google C++ Style Guide, the Namespaces section states that " Use of unnamed namespaces in header files can easily cause violations of the C++ One Definition Rule (ODR). " I understand why not using unnamed namespaces in an implementation file can cause ODR-violations, but not how use in a header can do so. How can this cause a violation? 回答1: The reason is that if you actually use anything in the anonymous namespace, you risk undefined behavior. For example: namespace { double const pi