language-lawyer

Is it legal to convert a pointer/reference to a fixed array size to a smaller size

不想你离开。 提交于 2020-12-04 14:56:07
问题 Is it legal as per the C++ standard to convert a pointer or reference to a fixed array (e.g. T(*)[N] or T(&)[N] ) to a pointer or reference to a smaller fixed array of the same type and CV qualification (e.g. T(*)[M] or T(&)[M] )? Basically, would this always be well-formed for all instantiations of T (regardless of layout-type): void consume(T(&array)[2]); void receive(T(&array)[6]) { consume(reinterpret_cast<T(&)[2]>(array)); } I don't see any references to this being a valid conversion in:

Is it legal to convert a pointer/reference to a fixed array size to a smaller size

你离开我真会死。 提交于 2020-12-04 14:55:43
问题 Is it legal as per the C++ standard to convert a pointer or reference to a fixed array (e.g. T(*)[N] or T(&)[N] ) to a pointer or reference to a smaller fixed array of the same type and CV qualification (e.g. T(*)[M] or T(&)[M] )? Basically, would this always be well-formed for all instantiations of T (regardless of layout-type): void consume(T(&array)[2]); void receive(T(&array)[6]) { consume(reinterpret_cast<T(&)[2]>(array)); } I don't see any references to this being a valid conversion in:

Is it legal to convert a pointer/reference to a fixed array size to a smaller size

蹲街弑〆低调 提交于 2020-12-04 14:54:18
问题 Is it legal as per the C++ standard to convert a pointer or reference to a fixed array (e.g. T(*)[N] or T(&)[N] ) to a pointer or reference to a smaller fixed array of the same type and CV qualification (e.g. T(*)[M] or T(&)[M] )? Basically, would this always be well-formed for all instantiations of T (regardless of layout-type): void consume(T(&array)[2]); void receive(T(&array)[6]) { consume(reinterpret_cast<T(&)[2]>(array)); } I don't see any references to this being a valid conversion in:

When passing a class by-value, does the caller or callee call the destructor?

橙三吉。 提交于 2020-12-01 09:36:43
问题 Suppose that I have the following (trimmed down) code: class P { P(); P(const P&); ~P(); } void foo(P x) { ... } void bar() { P p{}; foo(p); // compiler uses P::(const P&) to construct the value for x ... // compiler calls P::~P() on p } The compiler must create a copy of p in order to call foo , so the caller invokes the copy constructor before the call. My question is, who is in charge of destroying this created object? There seem to be two valid choices: The callee (i.e. foo ) calls the

When passing a class by-value, does the caller or callee call the destructor?

戏子无情 提交于 2020-12-01 09:36:00
问题 Suppose that I have the following (trimmed down) code: class P { P(); P(const P&); ~P(); } void foo(P x) { ... } void bar() { P p{}; foo(p); // compiler uses P::(const P&) to construct the value for x ... // compiler calls P::~P() on p } The compiler must create a copy of p in order to call foo , so the caller invokes the copy constructor before the call. My question is, who is in charge of destroying this created object? There seem to be two valid choices: The callee (i.e. foo ) calls the

How do c++ compilers find an extern variable?

試著忘記壹切 提交于 2020-12-01 09:10:16
问题 I compile this program by g++ and clang++. There has a difference: g++ prints 1, but clang++ prints 2. It seems that g++: the extern varible is defined in the shortest scope. clang++: the extern varible is defined in the shortest global scope. Does C++ spec has any specification about that? main.cpp #include <iostream> static int i; static int *p = &i; int main() { int i; { extern int i; i = 1; *p = 2; std::cout << i << std::endl; } } other.cpp int i; version: g++: 7.4.0/ clang++:10.0.0

Why is std::streamsize defined as signed rather than unsigned?

依然范特西╮ 提交于 2020-11-30 07:56:19
问题 According to http://en.cppreference.com/w/cpp/io/streamsize The type std::streamsize is a signed integral type used to represent the number of characters transferred in an I/O operation or the size of an I/O buffer. As far as I can imagine, a stream's size can never be negative, so, my question is: Why is std::streamsize defined as signed rather than unsigned ? What's the rationale behind? 回答1: The draft C++ standard has the following footnote 296 in section 27.5.2 Types which says:

Ambiguous operator overload on clang

徘徊边缘 提交于 2020-11-28 04:43:09
问题 When I try to compile this test program: struct comma_guard { template<class T> const comma_guard& operator,(T&&) const { return *this; } }; struct foo {}; template<class T> T operator,(T x, foo) { return x; } int main() { (comma_guard(), foo()); } I get a compile error on clang: comma_guard.cpp:20:19: error: use of overloaded operator ',' is ambiguous (with operand types 'comma_guard' and 'foo') (comma_guard(), foo()); ~~~~~~~~~~~~~^ ~~~~~ comma_guard.cpp:6:24: note: candidate function [with

Ambiguous operator overload on clang

拜拜、爱过 提交于 2020-11-28 04:42:31
问题 When I try to compile this test program: struct comma_guard { template<class T> const comma_guard& operator,(T&&) const { return *this; } }; struct foo {}; template<class T> T operator,(T x, foo) { return x; } int main() { (comma_guard(), foo()); } I get a compile error on clang: comma_guard.cpp:20:19: error: use of overloaded operator ',' is ambiguous (with operand types 'comma_guard' and 'foo') (comma_guard(), foo()); ~~~~~~~~~~~~~^ ~~~~~ comma_guard.cpp:6:24: note: candidate function [with

Ambiguous operator overload on clang

大兔子大兔子 提交于 2020-11-28 04:39:37
问题 When I try to compile this test program: struct comma_guard { template<class T> const comma_guard& operator,(T&&) const { return *this; } }; struct foo {}; template<class T> T operator,(T x, foo) { return x; } int main() { (comma_guard(), foo()); } I get a compile error on clang: comma_guard.cpp:20:19: error: use of overloaded operator ',' is ambiguous (with operand types 'comma_guard' and 'foo') (comma_guard(), foo()); ~~~~~~~~~~~~~^ ~~~~~ comma_guard.cpp:6:24: note: candidate function [with