one-definition-rule

Why is multiple definition of a const global variable allowed in C++ and not in C?

一世执手 提交于 2019-11-27 11:11:13
问题 Multiple definition of a global variable is not allowed in C or C++ due to the One Definition Rule. However, in C++ a const global variable can be defined in multiple compilation units with no error. This is not the same as in C. Why does C++ allow this while C does not? Why does the usage and behaviour of a const global differ from a non-const global in this way in C++ compared to C? What is happening under the covers with C++ and C with respect to const? For example this is allowed in C++,

When is a variable odr-used in C++14?

ぐ巨炮叔叔 提交于 2019-11-27 10:20:43
问题 The C++14 draft (N3936) states in §3.2/3: A variable x whose name appears as a potentially-evaluated expression ex is odr-used unless applying the lvalue-to-rvalue conversion (4.1) to x yields a constant expression (5.19) that does not invoke any non-trivial functions and, if x is an object, ex is an element of the set of potential results of an expression e, where either the lvalue-to-rvalue conversion (4.1) is applied to e, or e is a discarded-value expression (Clause 5). This doesn't make

What am I allowed to do with a static, constexpr, in-class initialized data member?

核能气质少年 提交于 2019-11-27 09:42:06
This is probably a bit of an unusual question, in that it asks for a fuller explanation of a short answer given to another question and of some aspects of the C++11 Standard related to it. For ease of reference, I shall sum up the referenced question here. The OP defines a class: struct Account { static constexpr int period = 30; void foo(const int &) { } void bar() { foo(period); } //no error? }; and is wondering why he gets no error about his usage of an in-class initialized static data member (a book mentioned this to be illegal). Johannes Schaub's answer states, that: This violates the One

Static constexpr odr-used or not?

蓝咒 提交于 2019-11-27 05:19:46
How come that the following works on gcc but doesn't on clang , ( see it live ): constexpr int giveMeValue() { return 42; } struct TryMe { static constexpr int arr[1] = { giveMeValue() }; }; int main() { int val = TryMe::arr[0]; return val; } I get an unresolved external symbol with clang. Is TryMe::arr[0] an object? If it is, is it odr-used? TryMe::arr is odr-used but you don't provide a definition ( see it live ): constexpr int TryMe::arr[1]; Why is the result inconsistent between gcc and clang ? This is because odr violations do not require a disagnostic, from both the C++11 and C++14 draft

If I don't odr-use a variable, can I have multiple definitions of it across translation units?

笑着哭i 提交于 2019-11-27 03:14:09
问题 The standard seems to imply that there is no restriction on the number of definitions of a variable if it is not odr-used (§3.2/3): Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required. It does say that any variable can't be defined multiple times within a translation unit (§3.2/1): No translation unit shall contain more than one definition of any variable, function, class type, enumeration type,

C the same global variable defined in different files

社会主义新天地 提交于 2019-11-26 19:19:39
问题 I am reading this code from here(in Chinese). There is one piece of code about testing global variable in C. The variable a has been defined in the file t.h which has been included twice. In file foo.c defined a struct b with some value and a main function. In main.c file, defined two variables without initialized. /* t.h */ #ifndef _H_ #define _H_ int a; #endif /* foo.c */ #include <stdio.h> #include "t.h" struct { char a; int b; } b = { 2, 4 }; int main(); void foo() { printf("foo:\t(&a)=0x

What am I allowed to do with a static, constexpr, in-class initialized data member?

本秂侑毒 提交于 2019-11-26 17:51:59
问题 This is probably a bit of an unusual question, in that it asks for a fuller explanation of a short answer given to another question and of some aspects of the C++11 Standard related to it. For ease of reference, I shall sum up the referenced question here. The OP defines a class: struct Account { static constexpr int period = 30; void foo(const int &) { } void bar() { foo(period); } //no error? }; and is wondering why he gets no error about his usage of an in-class initialized static data

Redeclaration of global variable vs local variable

偶尔善良 提交于 2019-11-26 16:18:24
问题 When I compile the code below #include<stdio.h> int main() { int a; int a = 10; printf("a is %d \n",a); return 0; } I get an error: test3.c: In function ‘main’: test3.c:6:5: error: redeclaration of ‘a’ with no linkage test3.c:5:5: note: previous declaration of ‘a’ was here But if I make the variable global then it works fine. #include<stdio.h> int a; int a = 10; int main() { printf("a is %d \n",a); return 0; } Why is declaring the same global variable twice not an error, but doing that for a

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

好久不见. 提交于 2019-11-26 12:53:56
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 lvalue-to-rvalue conversion (4.1) is immediately applied. in [basic.def.odr]. Edit: Apparently this is

Static constexpr odr-used or not?

纵然是瞬间 提交于 2019-11-26 12:47:41
问题 How come that the following works on gcc but doesn\'t on clang , ( see it live ): constexpr int giveMeValue() { return 42; } struct TryMe { static constexpr int arr[1] = { giveMeValue() }; }; int main() { int val = TryMe::arr[0]; return val; } I get an unresolved external symbol with clang. Is TryMe::arr[0] an object? If it is, is it odr-used? 回答1: TryMe::arr is odr-used but you don't provide a definition ( see it live ): constexpr int TryMe::arr[1]; Why is the result inconsistent between gcc