one-definition-rule

inline function in different translation units with different compiler flags undefined behaviour?

限于喜欢 提交于 2019-12-06 00:07:35
问题 in visual studio you can set different compiler options for individual cpp files. for example: under "code generation" we can enable basic runtime checks in debug mode. or we can change the floating point model (precise/strict/fast). these are just examples. there are plenty of different flags. an inline function can be defined multiple times in the program, as long as the definitions are identical. we put this function into a header and include it in several translation units. now, what

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

回眸只為那壹抹淺笑 提交于 2019-12-05 23:22:03
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++ standard if possible. Thanks! Merely including foo.h from multiple translation units will not violate

Scipy.Odr multiple variable regression

冷暖自知 提交于 2019-12-04 15:48:21
问题 I would like to perform a multidimensional ODR with scipy.odr . I read the API documentation, it says that multi-dimensionality is possible, but I cannot make it work. I cannot find working example on the internet and API is really crude and give no hints how to proceed. Here is my MWE: import numpy as np import scipy.odr def linfit(beta, x): return beta[0]*x[:,0] + beta[1]*x[:,1] + beta[2] n = 1000 t = np.linspace(0, 1, n) x = np.full((n, 2), float('nan')) x[:,0] = 2.5*np.sin(2*np.pi*6*t)+4

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

*爱你&永不变心* 提交于 2019-12-04 15:35:22
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 - #include "a.h" #include "b.h" extern A a; extern B b; int main() { a.Use(); b.Use(); } And the comment

Does redefining a function from the standard library violate the one-definition rule?

不羁的心 提交于 2019-12-04 10:59:33
#include <cmath> double log(double) {return 1.0;} int main() { log(1.0); } Suppose the function log() in <cmath> is declared in global namespace (this is unspecified in fact, and we just make this assumption), then it refers to the same function as the log() function we defined. So does this code violate the one-definition rule (see here , since no diagnostic required, this code may compile in some compiler and we cannot assert if it is correct)? Note : After recent edits, this is not a duplicate of: What exactly is One Definition Rule in C++? Typical scenario. If extern "C" double log(double)

How to avoid violating ODR with traits classes

好久不见. 提交于 2019-12-04 05:26:17
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 Traits for Something in one translation unit without including SpecialTraits.hpp and then instantiates a

About ODR-violations and template variables

不打扰是莪最后的温柔 提交于 2019-12-04 03:25:19
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? In other words, is i_am_odr_safe odr-safe? This is core issue 1713 , the direction of which IIRC is

GoogleTest PrintTo not getting called for a class

五迷三道 提交于 2019-12-04 02:59:17
问题 I'm having a rather strange problem telling googletest to print a certain class the way I want using PrintTo. The class is a very simple 2D point, it is in a namespace and the PrintTo function is in the same namespace. In fact, I have a derived class (a 3D point) which prints perfectly. Here's some code for the tests and PrintTo functions (namespace name edited, but everything else is copied and pasted from the actual code): // PrintTo Functions namespace MyNamespace { void PrintTo(const

Is there a way to detect inline function ODR violations?

天大地大妈咪最大 提交于 2019-12-04 00:04:20
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 (which requires -flto ) gold linker with -detect-odr-violations setting ASAN_OPTIONS=detect_odr_violation

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

*爱你&永不变心* 提交于 2019-12-03 14:22:21
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? The reason is that if you actually use anything in the anonymous namespace, you risk undefined behavior. For example: namespace { double const pi = 3.14159; } inline double twoPiR( double r ) { return 2.0 * pi * r; } The rule for inline functions (and