one-definition-rule

Is the main() function odr-used?

只谈情不闲聊 提交于 2020-01-15 11:55:06
问题 Is the main() function odr-used? E.g in the simple program like this: int main() { } 回答1: No, it is not. Not in your simple program. [basic.def.odr] 3 A function whose name appears as a potentially-evaluated expression is odr-used if it is the unique lookup result or the selected member of a set of overloaded functions ([basic.lookup], [over.match], [over.over]), unless it is a pure virtual function and either its name is not explicitly qualified or the expression forms a pointer to member (

A virtual member function is used if it is not pure?

被刻印的时光 ゝ 提交于 2020-01-12 07:42:07
问题 C++03 3.2.2 ...An object or non-overloaded function is used if its name appears in a potentially-evaluated expression. A virtual member function is used if it is not pure... And then later in 3.2.3 we have: Every program shall contain exactly one definition of every non-inline function or object that is used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is

Can consteval functions from different translation units interfere?

安稳与你 提交于 2020-01-06 05:46:07
问题 I am trying to dig into implications of a function being inline and stumbled upon this issue. Consider this small program (demo): /* ---------- main.cpp ---------- */ void other(); constexpr int get() { return 3; } int main() { std::cout << get() << std::endl; other(); } /* ---------- other.cpp ---------- */ constexpr int get() { return 4; } void other() { std::cout << get() << std::endl; } When compiled without optimizations, the program yields the following output: 3 3 Which might be not

Can you please explain this C++ delete problem?

本秂侑毒 提交于 2020-01-04 09:10:35
问题 I have the following code: std::string F() { WideString ws = GetMyWideString(); std::string ret; StringUtils::ConvertWideStringToUTF8(ws, ret); return ret; } WideString is a third-party class, so are StringUtils. They are a blackbox to me. Second parameter is passed by reference. When I step through the debugger the line return ret throws a nasty popup (Visual C++) saying that heap may be corrupted. Upon closer examination copy of the string that gets returned is OK, but the deletion of ret

Getting GCC linker to warn about multiple function definitions

浪尽此生 提交于 2020-01-04 04:30:11
问题 Consider my small example C library: #include <external_library.h> void some_function(void) { external_library_call(); // Do other stuff... } It plans to make some_function() publicly callable. The library doesn't work, though, because the external library it requires also happens to use a function called some_function(), which happens to have the same prototype. GCC's linker doesn't care how many sources of the some_function symbol there are, though. It picks one seemingly at random and the

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

99封情书 提交于 2020-01-01 12:08:21
问题 #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

Assign static constexpr class member to runtime variable

女生的网名这么多〃 提交于 2019-12-30 06:23:12
问题 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];

Assign static constexpr class member to runtime variable

三世轮回 提交于 2019-12-30 06:23:05
问题 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];

Identity of function template instantiation in multiple translation units

↘锁芯ラ 提交于 2019-12-23 22:25:34
问题 According to cppref, the identity characteristics of inline functions in multiple translation units are as follows: ... 2) It has the same address in every translation unit. 3) Function-local static objects in all function definitions are shared across all translation units (they all refer to the same object defined in one translation unit) ... Simply speaking, a singleton identity is implied. I'm wondering whether the same applies to function template instantiations without the inline

non-inlined virtual function defined in header file

谁说我不能喝 提交于 2019-12-23 21:26:57
问题 The One Definition Rule states that: In the entire program, an object or non-inline function cannot have more than one definition. (from Wikipedia) Well, I know that if an member function is defined in a header file, it's implicitly inlined, and it's ok with the ODR. But what about virtual functions? We know that if an virtual function is called polymorphically, it can't be inlined. if such a virtual function is deined in a header file, will that violate the ODR ? For instance: //derived.hpp