language-lawyer

#ifdef with multiple tokens, is this legal?

白昼怎懂夜的黑 提交于 2019-12-22 06:32:27
问题 Today I came across some C++ code that contains an #ifdef clause like this: #ifdef DISABLE_UNTIL OTHER_CODE_IS_READY foo(); #endif Note the space between "DISABLE_UNTIL" and "OTHER_CODE_IS_READY". Essentially there are two tokens specified in the #ifdef line. My question is, is this legal C++ code? (g++ compiles it without any errors, and it apparently just ignores the second token). And if it is legal, should the second token have any effect? 回答1: [C++11 16.1] , [C++11 16.5] and,

Computing the address of an object from the address of one of its member subobject

为君一笑 提交于 2019-12-22 05:16:49
问题 I am in the following situation: //This is Public class B{/*usefull stuff*/}; B*f(); void g(B*b)(); //Those classes are only declared the translation unit of f and g. class Whatever1{/*Implementation details only useful to f and g*/}; class Whatever2{/*Implementation details only useful to f and g*/}; class A{ public: Whatever1 w1; Whatever2 w2; B b; }; In function g, I want to convert the parameter (a pointer to B) to a pointer to A. B instances are always wrapped in A instances. I ended up

Same address, multiple shared_ptr counters, is it forbidden by C++ standard?

送分小仙女□ 提交于 2019-12-22 05:14:36
问题 Suppose I have the need to do the following (This is just some imaginative code for discussion of the C++ standard, thus I won't discuss why I design it this way, so don't bother me with something like: your design is wrong.) T* ptr = new T; shared_ptr<T> p(ptr); shared_ptr<T> q(ptr, SomeDeleterThatDoesnotDeleteButDoSomeOtherStuff()); Suppose the logic guarantees that p or some of its copies lives longer than all copies of q , so practically there won't be any problem. My question is, is it

Explicitly invoking `int` destructor - why is a type alias required? [duplicate]

橙三吉。 提交于 2019-12-22 05:10:55
问题 This question already has an answer here : Pseudo-destructor call does not destroy an object (1 answer) Closed 2 years ago . The following program... int main() { int{1}.~int(); } does not compile on (see conformance viewer) : clang++ trunk, with -std=c++1z g++ trunk, with -std=c++1z CL 19 2017 Introducing a type alias for int ... int main() { using X = int; int{1}.~X(); } ...makes the program valid on all previously mentioned compilers, without warnings (see conformance viewer) . Why is a

Does T D[N] always declare an object of array type?

南笙酒味 提交于 2019-12-22 05:10:30
问题 I'm confused about [dcl.array]/1: In a declaration T D where D has the form D1 [ constant-expression opt ] attribute-specifier-seq opt and the type of the identifier in the declaration T D1 is “ derived-declarator-type-list T”, then the type of the identifier of D is an array type; ... Consider the declaration: int (*p)[42]; This declaration satisfies the grammar described above (and does not satisfy the grammar described in previous paragraphs), so this paragraph should apply, thus we

The order of override and noexcept in the standard

风格不统一 提交于 2019-12-22 05:04:19
问题 Is the order of override and noexcept required by the standard? class Base { public: virtual void foo() {} }; class Derived : public Base { public: // virtual void foo() override {} // Ok // virtual void foo() noexcept {} // Ok // virtual void foo() noexcept override {} // Ok virtual void foo() override noexcept {} // Error }; int main() {} I'm using gcc 4.7.2. 回答1: Actually, yes, it is, its just hard to find out, since its a bit scattered. Annex A (Grammar summary) is of some help here. Lets

Is GCC correct in requiring the constexpr specifier for this reference declaration?

ε祈祈猫儿з 提交于 2019-12-22 04:46:14
问题 The code below doesn't compile under GCC 5.3.0 because the declaration of r is missing a constexpr specifier. const int i = 1; const int& r = i; constexpr int j = r; I believe the rejection is correct. How do I prove it using the working draft N4527? 回答1: First, since we're using a reference, [expr.const]/(2.9) must not be violated. (2.9.1) applies, though: an id-expression that refers to a variable or data member of reference type unless the reference has a preceding initialization and

Will template parameter deduction for constructors available since C++17 allow explicitly specify some of the class template arguments?

拟墨画扇 提交于 2019-12-22 04:43:17
问题 Apart from the most obvious usage of template parameter deduction for constructors, I can imagine some more complex use cases where we deduce only part of the parameters of the template class e.g.: std::pair<int> p(1, 2); // std::pair<int, int> Although this construct would be natural consequence of the deduction of template parameters in functions I couldn't find any example of this kind of usage. Maybe it's because of the ambiguity in case of classes with variadic template arguments? std:

Is round-trip through floating point always defined behavior if floating point range is bigger?

会有一股神秘感。 提交于 2019-12-22 04:37:24
问题 Let's say I have two arithmetic types, an integer one, I , and a floating point one, F . I also assume that std::numeric_limits<I>::max() is smaller than std::numeric_limits<F>::max() . Now, let's say I have a positive integer value i . Because the representable range of F is larger than I , F(i) should always be defined behavior. However, if I have a floating point value f such that f == F(i) , is I(f) well defined? In other words, is I(F(i)) always defined behavior? Relevant section from

Is it okay for int** and const int** to alias?

戏子无情 提交于 2019-12-22 04:32:39
问题 It is my understanding that something like this is okay: const int ci = 42; const int *cip = &ci; int *ip = (int *)cip; int j = *ip; What about this? const int ci = 42; const int *cip = &ci; const int **cipp = &cip; int **ipp = (int **)cipp; int j = **ipp; 回答1: The expression *ipp is an lvalue of type int * , however it is being used to access an object of effective type const int * . (Namely, cip ). According to the letter of the standard, it is a strict aliasing violation: the list of