language-lawyer

Template argument deduction: which compiler is right here?

为君一笑 提交于 2021-02-07 12:17:29
问题 Consider the following code: template<int N> class Vector { }; #include <array> template<int N> void doWork(const Vector<N>&, const std::array<int,N>&) { } int main() { std::array<int,3> arr; Vector<3> vec; doWork(vec,arr); } Here Vector represents a class which is defined in a third-party library, and std::array is known to take its element count as std::size_t . I've tried compiling this with clang-3.6 and g++-5.1. Clang worked without any complaint, while g++ gives the following error:

Template argument deduction: which compiler is right here?

左心房为你撑大大i 提交于 2021-02-07 12:15:31
问题 Consider the following code: template<int N> class Vector { }; #include <array> template<int N> void doWork(const Vector<N>&, const std::array<int,N>&) { } int main() { std::array<int,3> arr; Vector<3> vec; doWork(vec,arr); } Here Vector represents a class which is defined in a third-party library, and std::array is known to take its element count as std::size_t . I've tried compiling this with clang-3.6 and g++-5.1. Clang worked without any complaint, while g++ gives the following error:

Follow-up: What exactly is a variable in C++14/C++17?

ⅰ亾dé卋堺 提交于 2021-02-07 12:06:41
问题 As the title suggests, this question has been asked before. However, the answers pertained to C++03/0x(11). C++11 (N3337) says this about variables: [basic]/6: A variable is introduced by the declaration of a reference other than a non-static data member or of an object. The variable’s name denotes the reference or object. This may imply that variables are essentially named objects/references. However, in C++14/C++17, that last sentence was changed to The variable’s name, if any , denotes the

Should decltype(foo(1)) instantiate the constexpr function template foo?

…衆ロ難τιáo~ 提交于 2021-02-07 12:04:49
问题 The following code compiles with with gcc and MSVC, but fails using clang I tested with clang-3.5 and current trunk). template <typename T> constexpr auto wrong = false; template <typename T> constexpr auto foo(const T t) -> int { static_assert(wrong<T>, ""); return {}; } using F = decltype(foo(1)); int main() {} clang instantiates the function body and stumbles over the static_assert . gcc and MSVC just look at the function declaration and ignore the static_assert in the body. If you remove

Why is overloading on just one ref-qualifier not allowed?

时光怂恿深爱的人放手 提交于 2021-02-07 11:30:43
问题 Apparently, overloading on ref-qualifiers is not allowed – this code won't compile if you remove either & or && (just the tokens, not their functions): #include <iostream> struct S { void f() & { std::cout << "Lvalue" << std::endl; } void f() && { std::cout << "Rvalue" << std::endl; } }; int main() { S s; s.f(); // prints "Lvalue" S().f(); // prints "Rvalue" } In other words, if you have two functions of the same name and type, you have to define both if you define either . I assume this is

Downcast: why: ‘A’ is an inaccessible base of ‘B’?

我只是一个虾纸丫 提交于 2021-02-07 07:21:46
问题 Unlike other examples of this error message i already have a pointer to A and want to retrieve the actual child class. This kind of arrangement is part of some C++ wrapped C code there A is some POD C structure (whatswhy no dynamic cast) and test is some callback in C that calls C++ functionality and to retrieve the correct object the cast should be used. But to prevent C++ user code messing the C-Baseclass i would like to have the inheritance protected . MSVC does not complain about this but

Downcast: why: ‘A’ is an inaccessible base of ‘B’?

牧云@^-^@ 提交于 2021-02-07 07:21:37
问题 Unlike other examples of this error message i already have a pointer to A and want to retrieve the actual child class. This kind of arrangement is part of some C++ wrapped C code there A is some POD C structure (whatswhy no dynamic cast) and test is some callback in C that calls C++ functionality and to retrieve the correct object the cast should be used. But to prevent C++ user code messing the C-Baseclass i would like to have the inheritance protected . MSVC does not complain about this but

Friend, private function, template alias, and decltype… is clang correct in rejecting this?

こ雲淡風輕ζ 提交于 2021-02-07 06:55:25
问题 In the following code (godbolt link): #include <utility> struct Friend { class Inner { friend struct Friend; int function() { return 0; } }; using DirectResult = decltype(std::declval<Inner>().function()); template <typename T> using IndirectResult = decltype(std::declval<T>().function()); }; int main() { Friend::DirectResult direct{}; Friend::IndirectResult<Friend::Inner> indirect{}; return direct + indirect; } Clang is perfectly happy with the use of DirectResult , but will complaing that

Why compile-time floating point calculations might not have the same results as run-time calculations?

安稳与你 提交于 2021-02-07 06:45:06
问题 In constexpr: Introduction, the speaker mentioned "Compile-time floating point calculations might not have the same results as runtime calculations": And the reason is related to "cross-compiling". Honestly, I can't get the idea clearly. IMHO, different platforms may also have different implementation of integers. Why does it only affect floating points? Or I miss something? 回答1: You're absolutely right that, at some level, the problem of calculating floating-point values at compile time is

Overloading of hidden friends by differences only in (mutually exclusive) requires-clauses: legal or an ODR-violation?

有些话、适合烂在心里 提交于 2021-02-07 06:13:31
问题 Consider the following class template, which contains two (hidden) friend declarations of the same friend (same function type ; see below), which also defines the friend (and the friend is thus inline), but with the definition conditional on (mutually exclusive) requires-clauses : #include <iostream> struct Base {}; template<int N> struct S : public Base { friend int foo(Base&) requires (N == 1) { return 1; } friend int foo(Base&) requires (N == 2) { return 3; } }; [dcl.fct]/8 states that