language-lawyer

Is the omission of a forward declaration for a pointer to a structure valid? [duplicate]

十年热恋 提交于 2020-08-20 12:09:28
问题 This question already has answers here : How is it legal to reference an undefined type inside a structure? (7 answers) Closed last month . I recently came across this comment by @Paul Ogilvie: "You say "To define a pointer to a structure you only need to know the structure tag". In my experience that is unnecessary. Just declare a pointer_to_some_type and the compiler will reserve space for a pointer and does type checking on assignment. Only once you want dereference the pointer and access

Is it possible to call templated user-defined conversion operator with explicit template arguments?

三世轮回 提交于 2020-08-20 05:39:10
问题 Lets consider the following code (compiles successfully with clang++ 7.0.0 , compiler arguments are -std=c++17 -Wall -Wextra -Werror -pedantic-errors ): #include <iostream> struct Foo { template <typename Type = void> operator int() { return 42; } }; int main() { const auto i = Foo{}.operator int(); std::cout << i << std::endl; } Is it possible to call such templated user-defined conversion operator with explicitly provided template arguments? The naive approach doesn't compile: const auto i

Is it possible to call templated user-defined conversion operator with explicit template arguments?

懵懂的女人 提交于 2020-08-20 05:38:19
问题 Lets consider the following code (compiles successfully with clang++ 7.0.0 , compiler arguments are -std=c++17 -Wall -Wextra -Werror -pedantic-errors ): #include <iostream> struct Foo { template <typename Type = void> operator int() { return 42; } }; int main() { const auto i = Foo{}.operator int(); std::cout << i << std::endl; } Is it possible to call such templated user-defined conversion operator with explicitly provided template arguments? The naive approach doesn't compile: const auto i

Why C++ requires public inheritance, ignoring friend declarations, to make dynamic downcast working?

佐手、 提交于 2020-08-19 11:15:52
问题 Here we have a class B , inherited from class A , and it has a friend class C . Being a friend, C should have access to everything in B , including the A base class. To test it, first we create a B instance. we upcast its address to an A* then we try downcast it with dynamic_cast<> again to B* . The expected result is to get back the address of the original B instance. #include <cstdint> #include <cstdio> class A { public: virtual ~A() {}; }; class C; class B : protected A { // <- this should

Is this-> mandatory to access Base<T> identifiers from derived classes?

蹲街弑〆低调 提交于 2020-08-18 08:29:19
问题 This code compiles with MSVC 2015, but doesn't compile with Clang 5.0.0 (trunk 304874): template <typename T> struct Base { T data; }; template <typename T> struct Derived : Base<T> { auto getData() const { return data; } }; Replacing data with this->data in Derived::getdata() makes Clang happy. Which compiler is correct according to the C++ standard? Must this-> be used in template code to access an identifier of a base class? 回答1: Clang is correct. $17.6.2/3 Dependent names [temp.dep] In

Why gcc warns about narrowing conversion only for uniform initialization?

血红的双手。 提交于 2020-08-15 05:42:08
问题 I am trying to convert long type variable to int type variable with uniform initialization and without it. But I get compiler warning only with uniform initialization. Why is that? Why does not gcc warn in both cases? I have tried with clang also and got similar results. This is the code #include <iostream> int main() { long l = 1; int i1 = l; int i2 = { l }; std::cout << i1 << std::endl; std::cout << i2 << std::endl; return 0; } And the only one warning I get $ g++ -Wall -Wextra 1.cpp 1.cpp:

Why gcc warns about narrowing conversion only for uniform initialization?

烂漫一生 提交于 2020-08-15 05:42:08
问题 I am trying to convert long type variable to int type variable with uniform initialization and without it. But I get compiler warning only with uniform initialization. Why is that? Why does not gcc warn in both cases? I have tried with clang also and got similar results. This is the code #include <iostream> int main() { long l = 1; int i1 = l; int i2 = { l }; std::cout << i1 << std::endl; std::cout << i2 << std::endl; return 0; } And the only one warning I get $ g++ -Wall -Wextra 1.cpp 1.cpp:

Do async and await produce acquire and release semantics?

守給你的承諾、 提交于 2020-08-10 05:29:05
问题 I couldn't find a clear answer about whether returning from an async method always produces release semantics and whether await always produces acquire semantics. I assume yes, because otherwise any async/await code would be a minefield? So here's an example: are returned values both guaranteed to be 100*2 and 12345*2 , without any explicit locks or barriers? private static async Task<(int, int)> AMethod() { // Runs on the original thread: var x = 100; var y = 12345; var task = Task.Run(() =>

Is it a defect about deducing template arguments for function parameter pack

孤人 提交于 2020-08-07 21:36:53
问题 template<typename...T> void func(T...args){ } int main(){ func(1,2.0,'c'); } Consider the above code, there's a rule that applied to it to deduce these template arguments for this function template (call). It is: temp.deduct.call#1 For a function parameter pack that occurs at the end of the parameter-declaration-list, deduction is performed for each remaining argument of the call, taking the type P of the declarator-id of the function parameter pack as the corresponding function template

Is it a defect about deducing template arguments for function parameter pack

為{幸葍}努か 提交于 2020-08-07 21:29:28
问题 template<typename...T> void func(T...args){ } int main(){ func(1,2.0,'c'); } Consider the above code, there's a rule that applied to it to deduce these template arguments for this function template (call). It is: temp.deduct.call#1 For a function parameter pack that occurs at the end of the parameter-declaration-list, deduction is performed for each remaining argument of the call, taking the type P of the declarator-id of the function parameter pack as the corresponding function template