one-definition-rule

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

自古美人都是妖i 提交于 2019-11-28 17:16:12
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 any sense to me: If an expression e is a discarded-value expression depends on the context, in which e

One definition rule in c++

↘锁芯ラ 提交于 2019-11-28 12:16:04
问题 According to the c++ standard: No translation unit shall contain more than one definition of any variable, function, class type, enumeration type, or template. //--translation_unit.cpp--// int a; void foo() { int a; //Second defention of a. ODR fails. } Can you explain me how ODR does work actually? 回答1: This doesn't break the rule because you define two different variables. They have the same name, but are declared in different scopes, and so are separate entities. Each has a single

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

我怕爱的太早我们不能终老 提交于 2019-11-28 09:51:12
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, or template. But I can't find a restriction for non-odr-used variables across the entire program. So why

How to run ranlib on an archive built through Android.mk?

三世轮回 提交于 2019-11-28 09:17:46
问题 This has come up on a couple of libraries I work with regularly. See, for example: Error SSL archive symbol table (run ranlib) no archive symbol table (run ranlib) while building libcryptopp.a through ndk-build In the questions, the users created an Android.mk for the OpenSSL and Crypto++ libraries. The pain point seems to be users adding the Android.mk wrapper to the sources. Outside of Android, each project is Makefile based, each project builds a static archive, and each project builds a

anonymous namespaces and the one definition rule

好久不见. 提交于 2019-11-27 22:06:13
问题 Am I violating the One Definition Rule with the following program? // foo.hpp #ifndef FOO_HPP_ #define FOO_HPP_ namespace { inline int foo() { return 1; } } inline int bar() { return foo(); } #endif //EOF and // m1.cpp #include "foo.hpp" int m1() { return bar(); } //EOF and // m2.cpp #include "foo.hpp" int m2() { return bar(); } //EOF and finally // main.cpp #include <iostream> int m1(); int m2(); int main(int, const char* []) { int i = m1(); int j = m2(); std::cout << (i+j) << std::endl;

Why does explicit template instantiation not break ODR?

馋奶兔 提交于 2019-11-27 19:31:24
问题 This question arised in the context of this answer. As I would expect, this translation unit does not compile: template <int Num> int getNum() { return Num; } template int getNum<0>(); template int getNum<0>(); // error: duplicate explicit instantiation of 'getNum<0>' int main() { getNum<0>(); return 0; } I understand this, I have tried to make the same explicit template instantiation twice. However, it turns out that, separating this into different units, it compiles: // decl.h template <int

C the same global variable defined in different files

谁都会走 提交于 2019-11-27 18:03:01
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%08x\n\t(&b)=0x%08x\n \tsizeof(b)=%d\n\tb.a=%d\n\tb.b=%d\n\tmain:0x%08x\n", &a, &b, sizeof b, b.a, b.b

Can using a lambda in header files violate the ODR?

徘徊边缘 提交于 2019-11-27 17:56:25
Can the following be written in a header file: inline void f () { std::function<void ()> func = [] {}; } or class C { std::function<void ()> func = [] {}; C () {} }; I guess in each source file, the lambda's type may be different and therefore the contained type in std::function ( target_type 's results will differ). Is this an ODR ( One Definition Rule ) violation, despite looking like a common pattern and a reasonable thing to do? Does the second sample violate the ODR every time or only if at least one constructor is in a header file? Columbo This boils down to whether or not a lambda's

c & c++ default global variable linkage, multiple declaration & definition problem

冷暖自知 提交于 2019-11-27 13:32:56
问题 For example: code1.c / .cpp int a; // ... and so on code2.c / .cpp int a; int main(void) { return 0; } go to compile: $gcc code1.c code2.c # this is fine $ $g++ code1.cpp code2.cpp # this is dead /tmp/ccLY66HQ.o:(.bss+0x0): multiple definition of `a' /tmp/ccnIOmPC.o:(.bss+0x0): first defined here collect2: ld returned 1 exit status Is there any global variable linkage difference between C & C++? 回答1: It's not strictly legal. int a; is a tentative definition in C. You are allowed multiple

Redeclaration of global variable vs local variable

て烟熏妆下的殇ゞ 提交于 2019-11-27 13:20: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 local variable is an error? In C, the statement int a; when made at file scope, is a declaration and a