language-lawyer

How does the template argument deduction perform for function template parameter when it is a class template with default argument

孤街浪徒 提交于 2020-08-27 17:45:01
问题 template<typename T, typename U = T> struct Test{}; template<typename T> void func(Test<T>){ //#1 } int main(){ func(Test<int>{}); //#2 } Consider the above code, At the point of invocation of function template func , the type of argument is Test<int,int> , When call the function template, template argument deduction will perform. The rule of template argument deduction for function call is : temp.deduct#call-1 Template argument deduction is done by comparing each function template parameter

How does the template argument deduction perform for function template parameter when it is a class template with default argument

烈酒焚心 提交于 2020-08-27 17:43:10
问题 template<typename T, typename U = T> struct Test{}; template<typename T> void func(Test<T>){ //#1 } int main(){ func(Test<int>{}); //#2 } Consider the above code, At the point of invocation of function template func , the type of argument is Test<int,int> , When call the function template, template argument deduction will perform. The rule of template argument deduction for function call is : temp.deduct#call-1 Template argument deduction is done by comparing each function template parameter

How does the template argument deduction perform for function template parameter when it is a class template with default argument

孤街浪徒 提交于 2020-08-27 17:43:01
问题 template<typename T, typename U = T> struct Test{}; template<typename T> void func(Test<T>){ //#1 } int main(){ func(Test<int>{}); //#2 } Consider the above code, At the point of invocation of function template func , the type of argument is Test<int,int> , When call the function template, template argument deduction will perform. The rule of template argument deduction for function call is : temp.deduct#call-1 Template argument deduction is done by comparing each function template parameter

When to supply default arguments as template arguments

我的未来我决定 提交于 2020-08-27 08:35:38
问题 template<typename T, typename U = T> struct Test{}; template<typename T> void func(Test<T>){ //#1 } int main(){ func(Test<int>{}); //#2 } It seems to no rule in the standard that mentioned what situation the default argument is required for template-parameter. In dcl.fct.default#1 If an initializer-clause is specified in a parameter-declaration this initializer-clause is used as a default argument. Default arguments will be used in calls where trailing arguments are missing . In this section,

Anything in std::atomic is wait-free?

狂风中的少年 提交于 2020-08-24 08:09:29
问题 If T is a C++ fundamental type, and if std::atomic<T>::is_lock_free() returns true , then is there anything in std::atomic<T> that is wait-free (not just lock-free)? Like, load , store , fetch_add , fetch_sub , compare_exchange_weak , and compare_exchange_strong . Can you also answer based on what is specified in the C++ Standard, and what is implemented in Clang and/or GCC (your version of choice). My favorite definitions for lock-free and wait-free are taken from C++ Concurrency in Action

Accessing private data with reinterpret_cast

孤街浪徒 提交于 2020-08-22 11:56:46
问题 This seems to compile and access the private data successfully. Is this well-defined behavior? #include <iostream> #include <string> using std::string; class foo { string private_data = "Hello World"; }; int main() { foo f; auto* pprivate_data = reinterpret_cast<string*>(&f); std::cout << *pprivate_data << '\n'; } This question is sort of similar, but I believe it doesn't address my question. 回答1: No, the behavior is undefined. For such a reintepret_cast to have meaning, the two objects must

clang 5: std::optional instantiation screws std::is_constructible trait of the parameter type

本小妞迷上赌 提交于 2020-08-22 08:18:47
问题 A really strange and unexpected behaviour of clang 5 was detected when switching to c++17 and replacing custom std::optional solution with the standard one. For some reason, emplace() was being disabled due to faulty evaluation of a std::is_constructible trait of the parameter class. Some specific preconditions must be satisfied before it reproduces: #include <optional> /// Precondition #1: T must be a nested struct struct Foo { struct Victim { /// Precondition #2: T must have an aggregate

Is std::initializer_list{x, y, z} (CTAD) valid?

久未见 提交于 2020-08-22 03:23:07
问题 When constructing an std::initializer_list<U> explicitly, can the template argument ( U ) be deduced (using class template argument deduction (CTAD), for example)? In other words, I know that the following statements are valid: std::initializer_list<int> x1{1, 2, 3}; std::initializer_list<int> x2 = {1, 2, 3}; auto x3 = std::initializer_list<int>{1, 2, 3}; but are the following statements also valid? std::initializer_list x1{1, 2, 3}; std::initializer_list x2 = {1, 2, 3}; auto x3 = std:

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

喜夏-厌秋 提交于 2020-08-20 12:13:10
问题 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 the omission of a forward declaration for a pointer to a structure valid? [duplicate]

不羁岁月 提交于 2020-08-20 12:09:49
问题 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