language-lawyer

Does the boost::hold_any constructor have undefined behavior?

依然范特西╮ 提交于 2019-12-24 02:21:31
问题 I looked through the boost::hold_any file and I found something what confuses me. if I consider the informations I got through the answers to my question: What happens if you call a destructor and use the allocated memory again for other objects? (In the answer by Mike Seymour) then it is "forbidden" to manipulate the memory of an object which weren't released and reallocated yet, to place there a new object of a different type. I always thought the boost library sticks to the standard, but

Is object guaranteed to be initialized before calling its member function? [duplicate]

不羁岁月 提交于 2019-12-24 02:17:35
问题 This question already has answers here : static initialization order fiasco (4 answers) Closed last year . from this question (still not solved) I come across this new problem,so here I made an example: //main.cpp int main() { return 0; } //file1.cpp #include "b.h" B b; //file2.cpp #include "a.h" A a; //a.h #pragma once #include<iostream> #include "b.h" extern B b; class A { public: A(){ std::cout << "a cotr" << std::endl;b.Use(); } }; //b.h #pragma once #include<iostream> class B { public: B

Get the size of a member variable [duplicate]

独自空忆成欢 提交于 2019-12-24 01:13:22
问题 This question already has answers here : Does not evaluating the expression to which sizeof is applied make it legal to dereference a null or invalid pointer inside sizeof in C++? (3 answers) Is sizeof(*ptr) undefined behavior when pointing to invalid memory? (6 answers) Does the 'offsetof' macro from <stddef.h> invoke undefined behaviour? (6 answers) Why doesn't my program seg fault when I dereference a NULL pointer inside of malloc? (4 answers) Closed last year . There was a bug in g++-4.1

C++ template type specification

六月ゝ 毕业季﹏ 提交于 2019-12-24 01:07:26
问题 I have an empty class with the same name as a blank function. When I try to pass this class as a template parameter I receive an error: " type/value mismatch at argument 1 " " 'Test' is not a valid template type argument for parameter '_Ty' " Consider: #include <vector> void Test() { } class Test { }; int main() { std::vector<Test> test; } Changing to std::vector<class Test> seems to work, but I was not able to found out, whether this is required by a standard, or just randomly supported by

Using extern to refer to the instantiation of a non-static inline function

ⅰ亾dé卋堺 提交于 2019-12-24 01:01:58
问题 Is it possible to link with an instantiated non-static inline function by declaring it extern in another file?: inline.c: inline int foo(void) { return 42; } extern inline int foo(void); main.c: extern int foo(void); int main(){ return foo(); } Empirically $CC main.c inline.c (where CC is gcc , clang or tcc ) works. Is it a conforming C example? 回答1: The first question here is about the behaviour of extern inline int foo(void); in the same translation unit as the apparent inline definition.

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

How do I check whether character constants conform to ASCII?

半腔热情 提交于 2019-12-23 19:33:55
问题 A comment on an earlier version of this answer of mine alerted me to the fact that I can't assume that 'A' , 'B' , 'C' etc. have successive numeric values. I had sort of assumed the C or C++ language standards guarantee that this is the case. So, how should I determine whether consecutive letter characters' values are themselves consecutive? Or rather, how can I determine whether the character constants I can express within single quotes have their ASCII codes for a numeric value? I'm asking

C++14 lambda's default argument type deduction depending on preceding arguments

两盒软妹~` 提交于 2019-12-23 19:05:24
问题 Is this not valid as C++14? auto f = [](auto x, auto y = std::decay_t<decltype(x)>{}) { }; f(0); I was expecting it to be roughly equivalent to auto f = [](int x, int y) { }; f(0, int{}); Neither GCC 6.3 nor Clang 4.0 accepted my code. http://ideone.com/b7b4SK GCC http://ideone.com/EyLYaL Clang Is it related to my lack of understanding of C++ template deduction phases? Does the 1400 pages long spec actually has an explicit answer to my question? Update To summarize, my problem can in fact be

Forward declare entities in C standard library?

孤街浪徒 提交于 2019-12-23 18:53:15
问题 Is it legal to forward declare structs and functions provided by the C standard library? My background is C++ in which the answer is no. The primary reason for this is that a struct or class mandated by the C++ standard library can be a template behind the scenes and may have "secret" template parameters and so cannot be properly declared with a naive non-template declaration. Even if a user does figure out exactly how to forward declare a particular entity in a particular version of a

Best/proper way to define uint64_t constants

放肆的年华 提交于 2019-12-23 18:45:05
问题 constexpr auto v = static_cast<std::uint64_t>(1) << 32; is not ideal, because of the tedious syntax and the cast which is semantically indirect. From this thread, I learned constexpr auto v = UINT64_C(1) << 32; However, the precise semantics of the macro is expands to an integer constant expression having the value specified by its argument and the type uint_least64_t . Therefore, it's not exactly uint64_t . I'm wondering what is the best/proper way to define uint64_t constants. Note that