one-definition-rule

Impossibly Fast C++ Delegates and different translation units

折月煮酒 提交于 2019-12-03 04:22:38
问题 According to Sergey Ryazanov, his Impossibly Fast C++ Delegates are not comparable: My delegates cannot be compared. Comparison operators are not defined because a delegate doesn't contain a pointer to method. Pointer to a stub function can be different in various compilation units. To which one the readers have replied: "Pointer to a stub function can be different in various compilation units." AFAIK, this is not true. Compilers are required to re-use template functions generated in

Is static constexpr variable odr-used?

隐身守侯 提交于 2019-12-01 23:56:21
问题 Giving below code, is Foo::FOO1 ODR-used or not? #include <iostream> #include <map> #include <string> class Foo { public: static constexpr auto FOO1 = "foo1"; void bar(); }; void Foo::bar() { const std::map<std::string, int> m = { {FOO1, 1}, }; for (auto i : m) { std::cout << i.first << " " << i.second << std::endl; } } int main() { Foo f; f.bar(); return 0; } Compiling the code with -O1 or above, it is OK, but if compile with -O0 , I get below error (see coliru example: undefined reference

passing a static constexpr variable by universal reference?

我的未来我决定 提交于 2019-12-01 18:15:30
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 <typename T> void ref(T&& x) { cout << std::forward<T>(x) << endl; } template <typename T> void val(T x

GoogleTest PrintTo not getting called for a class

不想你离开。 提交于 2019-12-01 16:00:10
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 MyNamespace::CPunto2D& pto, ::std::ostream* os) { *os << "("; *os << pto.X(); *os << ","; *os << pto.Y();

C++ - Define member function outside template-class but in header

。_饼干妹妹 提交于 2019-12-01 11:56:02
I have defined a simple class-template with one member function. It is defined outside the class with an additional (explicit) specialization, also defined outside the class. All in one headerfile. If you include this header in multiple translation units you get a linker error due to One-Definition-Rule. // Header with a template template <class T> class TestClass { public: TestClass() {}; ~TestClass() {}; bool MemberFunction(); }; template <class T> bool TestClass<T>::MemberFunction() { return true; } template <> bool TestClass<double>::MemberFunction() { return true; }; Everything fine so

What means “obey ODR” in case of inline and constexpr function?

别等时光非礼了梦想. 提交于 2019-12-01 06:43:10
I just read that constexpr and inline functions obey one-definition rule, but they definition must be identical. So I try it: inline void foo() { return; } inline void foo() { return; } int main() { foo(); }; error: redefinition of 'void foo()', and constexpr int foo() { return 1; } constexpr int foo() { return 1; } int main() { constexpr x = foo(); }; error: redefinition of 'constexpr int foo()' So what exactly means that, constexpr and inline function can obey ODR? I just read that constexpr and inline functions obey one-definition rule, but they definition must be identical. This is in

What means “obey ODR” in case of inline and constexpr function?

落花浮王杯 提交于 2019-12-01 04:51:31
问题 I just read that constexpr and inline functions obey one-definition rule, but they definition must be identical. So I try it: inline void foo() { return; } inline void foo() { return; } int main() { foo(); }; error: redefinition of 'void foo()', and constexpr int foo() { return 1; } constexpr int foo() { return 1; } int main() { constexpr x = foo(); }; error: redefinition of 'constexpr int foo()' So what exactly means that, constexpr and inline function can obey ODR? 回答1: I just read that

Where should the definition of an explicit specialization of a class template be placed in C++?

萝らか妹 提交于 2019-12-01 00:17:06
问题 According to [temp.spec]/5: For a given template and a given set of template-arguments, ... an explicit specialization shall be defined at most once in a program (according to [basic.def.odr]), and ... the definition of an explicit (full) specialization of a class template cannot be placed in a header (otherwise there is one definition in each translation unit containing this header, thus there will be more than one definition in the whole program). In addition, as another evidence, the

using constants in header file with ODR compliance

霸气de小男生 提交于 2019-11-30 23:49:37
Looking at another question I realized that I can't use objects or functions from an anonymous namespace through a header file since it'll cause ODR violations in class definitions or inline functions. If this is the case, then is it possible to use named const or constexpr static objects in inline functions or in classes safely? For example, if CONSTANT was inside of namespace below it would be unsafe, but is it okay to use a constant with static linkage? // some header file to be included by multiple .cpp files static const/*expr*/ int CONSTANT = 2; inline int f() { return CONSTANT; } class

Assign static constexpr class member to runtime variable

我怕爱的太早我们不能终老 提交于 2019-11-30 19:33:56
I know there are a lot of similar questions, but somehow different questions. It is about the following situation: #include <iostream> #include <array> template<typename T> class MyClass { public: static constexpr std::array<T,4> ARRAY {{4, 3, 1, 5}}; }; int main() { constexpr std::array<int, 4> my_array(MyClass<int>::ARRAY); // works fine -> can use the ARRAY to initialize constexpr std::array constexpr int VALUE = 5*MyClass<int>::ARRAY[0]; // works also fine int value; value = my_array[0]; // can assign from constexpr value = MyClass<int>::ARRAY[0]; // undefined reference to `MyClass<int>: