c++17

Function template overload resolution, dependent and non-dependent parameters

一曲冷凌霜 提交于 2021-02-07 02:07:22
问题 Given the following program #include <iostream> template<class T> struct id { using type = T; }; template<class T1, class T2> int func(T1, T2) { return 0; } template<class T1, class T2> int func(typename id<T1>::type, typename id<T2>::type) { return 1; } int main() { std::cout << func<int, int>(0, 0) << std::endl; } GCC and Clang both prints 1 for this program. Is this program guaranteed to print 1 by the standard? I tried finding the answer here but couldn't decipher it. It looks like the

Should I make my local variables const or movable?

纵然是瞬间 提交于 2021-02-06 15:26:27
问题 My default behaviour for any objects in local scopes is to make it const . E.g.: auto const cake = bake_cake(arguments); I try to have as little non-functional code as I can as this increases readability (and offers some optimisation opportunities for the compiler). So it is logical to also reflect this in the type system. However, with move semantics, this creates the problem: what if my cake is hard or impossible to copy and I want to pass it out after I'm done with it? E.g.: if (tastes

Should I make my local variables const or movable?

我们两清 提交于 2021-02-06 15:25:12
问题 My default behaviour for any objects in local scopes is to make it const . E.g.: auto const cake = bake_cake(arguments); I try to have as little non-functional code as I can as this increases readability (and offers some optimisation opportunities for the compiler). So it is logical to also reflect this in the type system. However, with move semantics, this creates the problem: what if my cake is hard or impossible to copy and I want to pass it out after I'm done with it? E.g.: if (tastes

Should I make my local variables const or movable?

本小妞迷上赌 提交于 2021-02-06 15:24:20
问题 My default behaviour for any objects in local scopes is to make it const . E.g.: auto const cake = bake_cake(arguments); I try to have as little non-functional code as I can as this increases readability (and offers some optimisation opportunities for the compiler). So it is logical to also reflect this in the type system. However, with move semantics, this creates the problem: what if my cake is hard or impossible to copy and I want to pass it out after I'm done with it? E.g.: if (tastes

Is there a nice way to assign std::minmax(a, b) to std::tie(a, b)?

天涯浪子 提交于 2021-02-06 06:29:11
问题 std::tie(a, b) = std::minmax(a, b); I think this is intuitive code. Clean and understandable. Too bad it doesn't work as intended, as std::minmax templates for const& . If therefore the values are swapped inside the std::pair<const&, const&> than one assignement will overwrite the other value: auto[a, b] = std::make_pair(7, 5); std::tie(a, b) = std::minmax(a, b); std::cout << "a: " << a << ", b: " << b << '\n'; a: 5, b: 5 The expected output here is a: 5, b: 7 . I think this is important as

Is there a reason to use std::conjunction/std::disjunction instead of a fold expression over “&&”/“||”?

风流意气都作罢 提交于 2021-02-05 19:58:17
问题 Is there any specific cases you cannot correctly do with std::conjunction / std::disjunction and not using the more "fundamental" (i.e. language feature instead of library feature) fold expression over && / || ? Example: // func is enabled if all Ts... have the same type template<typename T, typename... Ts> std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...> > func(T, Ts...) { // TODO something to show } vs // func is enabled if all Ts... have the same type template<typename T,

Is there a reason to use std::conjunction/std::disjunction instead of a fold expression over “&&”/“||”?

一笑奈何 提交于 2021-02-05 19:56:48
问题 Is there any specific cases you cannot correctly do with std::conjunction / std::disjunction and not using the more "fundamental" (i.e. language feature instead of library feature) fold expression over && / || ? Example: // func is enabled if all Ts... have the same type template<typename T, typename... Ts> std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...> > func(T, Ts...) { // TODO something to show } vs // func is enabled if all Ts... have the same type template<typename T,

Does Lvalue-to-Rvalue conversion occur in function invocation?

混江龙づ霸主 提交于 2021-02-05 09:18:45
问题 Consider the below code: #include <iostream> int func(){ int a = 0; return a; } int main(){ int result = func(); } According to the cpp standard, some rules about the return statement are: A function returns to its caller by the return statement. [...] the return statement initializes the glvalue result or prvalue result object of the (explicit or implicit) function call by copy-initialization from the operand So, the invocation for int result = func(); , as if it could be translate to: //a

structured binding of member variables

纵饮孤独 提交于 2021-02-05 07:52:16
问题 In visual studio 2017, after turning on /std:c++17 , I can do auto [d1, d2] = func_return_tuple(); , where func_return_tuple() returns a tuple of two double value. However, if class A has two member variable d1_ and d2_ , I can't do A a; auto [a.d1_, a.d2_] = func_return_tuple(); Is there a way out? Of course, std::tie(a.d1_, a.d2_) = func_return_tuple(); always works. 回答1: Is there a way out? Basically, no. Structured bindings is only about decomposing an object into constituent elements. It

c++ - const member func, that can be called upon lvalue instances only, using a ref-qualifier

醉酒当歌 提交于 2021-02-05 07:48:31
问题 I'm trying to enforce a const 'getter' method of a class to be called upon only lvalue instances of the class, via a ref-qualifier and for some reason getting an unexpected result (I'm compiling with clang 6.0.1 with C++ 17 support, via c++1z flag, on Windows ): The declaration bool getVal() const &; allows the method to be called on rvalue references also . The declaration bool getVal() &; doesn't allow the method to be called on rvalue references BUT, as I understand - the function isn't a