one-definition-rule

What does it mean to “ODR-use” something?

依然范特西╮ 提交于 2019-12-17 02:09:13
问题 This just came up in the context of another question. Apparently member functions in class templates are only instantiated if they are ODR-used. Could somebody explain what exactly that means. The wikipedia article on One Definition Rule (ODR) doesn't mention " ODR-use ". However the standard defines it as A variable whose name appears as a potentially-evaluated expression is odr-used unless it is an object that satisfies the requirements for appearing in a constant expression (5.19) and the

Continuation of “I just can not understand DR 712”

一世执手 提交于 2019-12-14 03:45:51
问题 This is basically a continuation of my prior question about DR 712. Let me first explain why I'm insisting in looking at something that could be considered old, as the C++11 Standard, but my problem is that the section [basic.def.odr] is already difficult to understand in C++11, and I want to cover this completely before I delve into the same section in the current draft, which in my opinion, is even more complicated. The answer to my prior question by Austing Hastings was great, but I still

Why doesn't calling member function invoke the ODR-USE of that object?

℡╲_俬逩灬. 提交于 2019-12-13 12:12:39
问题 Here in cppref says, If the initialization of a non-inline variable (since C++17) is deferred to happen after the first statement of main/thread function, it happens before the first odr-use of any variable with static/thread storage duration defined in the same translation unit as the variable to be initialized. And later it gives an example of deferred dynamic initialization: // - File 1 - #include "a.h" #include "b.h" B b; A::A(){ b.Use(); } // - File 2 - #include "a.h" A a; // - File 3 -

C++ standard: ODR and constexpr std::string_view

时光怂恿深爱的人放手 提交于 2019-12-12 09:55:16
问题 If I have a header foo.h which contains #ifndef FOO_H_ #define FOO_H_ namespace foo { constexpr std::string_view kSomeString = "blah"; } #endif // FOO_H_ then is it safe to include foo.h from within multiple .cc files in a single program, regardless of what they do with the symbol kSomeString , or are there some uses that could cause an ODR violation? Also, is it guaranteed that kSomeString.data() will return the same pointer across .cc files? I'd like specific references to wording in the C+

C++ confusion about one definition rule

旧城冷巷雨未停 提交于 2019-12-11 11:34:12
问题 I was reading about One Definition Rule. It says that: if one .cpp file defines struct S { int x; }; and the other .cpp file defines struct S { int y; }; , the behavior of the program that links them together is undefined. This is usually resolved with unnamed namespaces. I don't understand why & how it is undefined? Will someone explain me the actual reason behind this? How it is resolved with unnamed namespaces? 回答1: It's just as it says. You defined the same class S twice, with different

How to define a class in a source file and declare it in a header file (without having to define the class methods using `class::method` syntax)?

匆匆过客 提交于 2019-12-11 07:42:22
问题 I am looking at a library on github which has the following in one of the header files: class Util { public: static void log( const string& message ); static void log( const char* message ); template<typename T> static inline string toString(T t) { stringstream s; s << t; return s.str(); } }; and the following in the source file: void Util::log( const string& message ) { const string& logMessage = "[cppWebSockets] " + message; syslog( LOG_WARNING, "%s", logMessage.c_str( ) ); } void Util::log

One Definition Rule: Can corresponding entities have different names?

旧城冷巷雨未停 提交于 2019-12-11 03:55:33
问题 I read and reread the relevant clauses about ODR in the C++ standard, but this question still remains open to me. The standard says that the definition of an inline function shall appear in every translation unit in which it is used, and the definitions shall be identical in a sense which is described in almost a page. It says the token sequence must be the same. Does it include the local identifier names? In other words does the following program violate the ODR? (I tried to test it myself

Forcing initialization of static data member of template class

人盡茶涼 提交于 2019-12-11 03:42:34
问题 There have been a few questions about static data members of template classes not being initialized. Unfortunately none of these had answers that were able to help me with my specific problem. I have a template class that has a static data member that must be instantiated explicitly for specific types (i.e., must be specialized). If this is not the case, using a different template function should cause a linker error. Here's some code: #include <iostream> template <typename T> class

Access static constexpr std::array without out-of-class definition

懵懂的女人 提交于 2019-12-11 02:38:24
问题 I have a class that defines some arrays. Points.hpp class Points { public: static constexpr std::array< double, 1 > a1 = { { +0.0 } }; static constexpr std::array< double, 2 > a2 = { { -1.0 / std::sqrt( 3.0 ), +1.0 / std::sqrt( 3.0 ) } }; }; My main file then uses these arrays. main.cpp #include "Points.hpp" int main() { // Example on how to access a point. auto point = Points::a2[0]; // Do something with point. } When I compile my code, using C++11 and g++ 4.8.2, I get the following linker

Inlining causes specialized member function of template class overriding virtual functions to get overlooked

微笑、不失礼 提交于 2019-12-10 17:13:36
问题 I wanted to share a strange example with you guys that I stumbled upon and that kept me thinking for two days. For this example to work you need: triangle-shaped virtual inheritance (on member function getAsString() ) member function specialization of a template class (here, Value<bool>::getAsString() ) overriding the virtual function (automatic) inlining by the compiler You start with a template class that virtually inherits a common interface - i.e. a set of virtual functions. Later, we