one-definition-rule

Violating the one definition rule by simply linking dynamically

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-11 08:09:14
问题 Question: Are dynamically linked C++ programs on ELF platforms always on the brink of producing undefined behavior by violating the one definition rule? More specific: By simply writing a shared library exposing one function #include <string> int __attribute__((visibility("default"))) combined_length(const char *s, const char *t) { const std::string t1(t); const std::string u(s + t1); return u.length(); } and compiling it with GCC 7.3.0 via $ g++ -Wall -g -fPIC -shared \ -fvisibility=hidden

Violating the one definition rule by simply linking dynamically

孤者浪人 提交于 2021-02-11 08:08:04
问题 Question: Are dynamically linked C++ programs on ELF platforms always on the brink of producing undefined behavior by violating the one definition rule? More specific: By simply writing a shared library exposing one function #include <string> int __attribute__((visibility("default"))) combined_length(const char *s, const char *t) { const std::string t1(t); const std::string u(s + t1); return u.length(); } and compiling it with GCC 7.3.0 via $ g++ -Wall -g -fPIC -shared \ -fvisibility=hidden

Violating the one definition rule by simply linking dynamically

痞子三分冷 提交于 2021-02-11 08:07:37
问题 Question: Are dynamically linked C++ programs on ELF platforms always on the brink of producing undefined behavior by violating the one definition rule? More specific: By simply writing a shared library exposing one function #include <string> int __attribute__((visibility("default"))) combined_length(const char *s, const char *t) { const std::string t1(t); const std::string u(s + t1); return u.length(); } and compiling it with GCC 7.3.0 via $ g++ -Wall -g -fPIC -shared \ -fvisibility=hidden

Violating the one definition rule by simply linking dynamically

孤人 提交于 2021-02-11 08:07:28
问题 Question: Are dynamically linked C++ programs on ELF platforms always on the brink of producing undefined behavior by violating the one definition rule? More specific: By simply writing a shared library exposing one function #include <string> int __attribute__((visibility("default"))) combined_length(const char *s, const char *t) { const std::string t1(t); const std::string u(s + t1); return u.length(); } and compiling it with GCC 7.3.0 via $ g++ -Wall -g -fPIC -shared \ -fvisibility=hidden

Violating the one definition rule by simply linking dynamically

不打扰是莪最后的温柔 提交于 2021-02-11 08:06:55
问题 Question: Are dynamically linked C++ programs on ELF platforms always on the brink of producing undefined behavior by violating the one definition rule? More specific: By simply writing a shared library exposing one function #include <string> int __attribute__((visibility("default"))) combined_length(const char *s, const char *t) { const std::string t1(t); const std::string u(s + t1); return u.length(); } and compiling it with GCC 7.3.0 via $ g++ -Wall -g -fPIC -shared \ -fvisibility=hidden

Why C++'s <vector> templated class doesn't break one definition rule?

不打扰是莪最后的温柔 提交于 2021-02-04 17:18:22
问题 Maybe its lame question, But I don't get it! If I include <string> or <vector> in multiple translation units (different .cpp) why it doesn't break the ODR? As far as I know each .cpp is compiled differently so vector's methods code will be generated for each object file separately, right? So linker should detect it and complain. Even If it won't (I suspect it's special case for templates) will it be using one code or different set of cloned code in each unit, when I link all together??? 回答1:

Why One Definition Rule, not One Declaration Rule?

我与影子孤独终老i 提交于 2021-01-27 04:45:11
问题 I have read materials below: https://www.wikiwand.com/en/One_Definition_Rule http://en.cppreference.com/w/cpp/language/definition What is the difference between a definition and a declaration? But still, can't figure out why it is One Definition Rule rather than One Declaration Rule? I maintain that declaration is a subset of definition, so One Definition Rule is enough. 回答1: Definition is a subset of declaration, not the other way around. Every definition is a declaration, and there are

Why doesn't the compiler warn against ODR violations in the same translation unit

我们两清 提交于 2020-07-05 11:38:05
问题 In the same translation unit, ODR problems are easy to diagnose. Why then does the compiler not warn against ODR violations in the same translation unit? For example in the following code https://wandbox.org/permlink/I0iyGdyw9ynRgny6 (reproduced below), there is an ODR violation with detecting if std::tuple_size has been defined. And further the undefined behavior is evident when you uncomment the defintiions of three and four . The output of the program changes. Just trying to understand why

emplace_back causes link error on static constexpr member

ⅰ亾dé卋堺 提交于 2020-07-03 05:50:39
问题 Why does emplace_back take a reference of the member that requires a definition? What is the difference between emplace_back(integer literal) and emplace_back(static constexpr integer member) ? If I switch to C++17, it compiles fine. I found that in C++17 static constexpr data members are implicitly inlined. Does it mean the compiler implicitly creates a definition for them? Example code: class base { int n; public: base(int n):n(n) {} }; struct base_trait { static constexpr int n = 1; }; int

inline function and class and header file

℡╲_俬逩灬. 提交于 2020-06-27 11:59:14
问题 Will any function defined in the header file automatically be inline? If I declare a function in a class and give the definition outside using keyword inline, will this function be inline? If it is, why this does not against the law that inline function should be given the body at declaration? 回答1: Any function defined inside a class definition is inline. Any function marked inline is also inline. class C { int f() { return 3; } // inline int g(); int h(); } inline int C::g() { return 4; } //