return-type-deduction

Why doesn't std::is_invocable work with templated operator() which return type is auto-deduced (eg. generic lambdas)

女生的网名这么多〃 提交于 2021-02-08 09:47:36
问题 c++17 introduces template <class Fn, class...ArgTypes> struct is_invocable: Determines whether Fn can be invoked with the arguments ArgTypes... . Formally, determines whether INVOKE(declval<Fn>(), declval<ArgTypes>()...) is well formed when treated as an unevaluated operand, where INVOKE is the operation defined in Callable . However, this template does not work with templated operator() which (direct or indirect) return type is auto-deduced: #include <type_traits> #include <iostream> struct

Return type deduction for in-class friend functions

大城市里の小女人 提交于 2020-01-21 02:36:10
问题 Here is a little experiment with return type deduction for in-class friend functions (using Clang 3.4 SVN and g++ 4.8.1 with std=c++1y in both cases) that is not documented in the linked working paper #include <iostream> struct A { int a_; friend auto operator==(A const& L, A const& R) { return L.a_ == R.a_; // a_ is of type int, so should return bool } }; template<class T> struct B { int b_; friend auto operator==(B const& L, B const& R) { return L.b_ == R.b_; // b_ is of type int, so should

Does a placeholder in a trailing-return-type override an initial placeholder?

∥☆過路亽.° 提交于 2020-01-14 08:27:09
问题 g++ appears to accept any combination of auto and decltype(auto) as initial and trailing return types: int a; auto f() { return (a); } // int auto g() -> auto { return (a); } // int auto h() -> decltype(auto) { return (a); } // int& decltype(auto) i() { return (a); } // int& decltype(auto) j() -> auto { return (a); } // int decltype(auto) k() -> decltype(auto) { return (a); } // int& However, clang rejects j and k , saying: error: function with trailing return type must specify return type

Is it possible to ignore the trailing return type feature of c++11 in favor of the function return type deduction feature of c++14?

戏子无情 提交于 2020-01-02 00:56:14
问题 When I skip the return type of an expression The following code in C++11 : auto function(X x, Y y) -> decltype(x + y) { return x + y; } Is equal to the following code in C++14 : decltype(auto) function(X x, Y y) { return x + y; } But additionally it is possible to deduce the return type without decltype rules in C++14 : auto function() { return 0; } When I know what the return type is exactly The following code in C++11 : auto function() -> int { return 0; } Is equal to the following code in

Why can the return type of main not be deduced?

守給你的承諾、 提交于 2019-12-23 06:44:49
问题 As expected, the following fails in C++11 because that language does not have return type deduction for bog standard functions: auto main() { return 0; } However, C++14 does, so I cannot explain the following error (with equivalent outcomes in GCC trunk, clang 3.8 and Visual Studio 2015): error: 'main' must return 'int' Is there a passage in the standard that I'm not seeing, forbidding return type deduction for main ? Or are both compilers non-compliant? (For what it's worth, I'd never

Legitimate uses of the trailing return type syntax as of C++14

倾然丶 夕夏残阳落幕 提交于 2019-12-21 04:31:27
问题 Is there actually any reason to use the following syntax anymore : template<typename T> auto access(T& t, int i) -> decltype(t[i]) { return t[i]; } Now that we can use : template<typename T> decltype(auto) access(T& t, int i) { return t[i]; } The trailing return type syntax now seems a little redundant? 回答1: Deduced return types are not SFINAE friendly. This overload will simply drop out of the overload set if t[i] is invalid: template<typename T> auto access(T& t, int i) -> decltype(t[i]) {

CRTP and c++1y return type deduction

女生的网名这么多〃 提交于 2019-12-21 03:31:07
问题 I was recently playing with CRTP when I came across something that surprised me when used with c++1y functions whose type is deduced. The following code works: template<typename Derived> struct Base { auto foo() { return static_cast<Derived*>(this)->foo_impl(); } }; struct Derived: public Base<Derived> { auto foo_impl() -> int { return 0; } }; int main() { Derived b; int i = b.foo(); (void)i; } I assumed that the return type from Base<Derived>::foo was a decltype of the expression returned,

Why does “return (str);” deduce a different type than “return str;” in C++?

筅森魡賤 提交于 2019-12-20 18:30:03
问题 Case 1: #include <iostream> decltype(auto) fun() { std::string str = "In fun"; return str; } int main() { std::cout << fun() << std::endl; } Here, program work fine in Gcc Compiler. decltype(auto) is deduced to be the type of str . Case 2: #include <iostream> decltype(auto) fun() { std::string str = "In fun"; return (str); // Why not working?? } int main() { std::cout << fun() << std::endl; } Here, generated following error and Segmentation fault : In function 'decltype(auto) fun()': prog.cc

construction helper make_XYZ allowing RVO and type deduction even if XZY has noncopy constraint

萝らか妹 提交于 2019-12-17 18:29:40
问题 UPDATE1: C++17 added type deduction for constructors - which does not imply that the free function is an inferior solution. UPDATE2: C++17 added guaranteed copy elision (the copy does not even take place conceptually). So with C++17 my code actually works and with optimal performance. But Martinho's code using brace initialisation for the return value is still the cleaner solution I believe. But checkout this answer from Barry and the comment from T.C. OLD POST: Type deduction does not work

How do I deduce auto before a function is called?

烈酒焚心 提交于 2019-12-10 12:32:20
问题 while experimenting with function return type deduction auto func(); int main() { func(); } auto func() { return 0; } error: use of ‘auto func()’ before deduction of ‘auto’ Is there a way to use this feature without needing to specify the definition before the call? With a large call tree, it becomes complicated to re-arrange functions so that their definition is seen before all of the places they are called. Surely an evaluation could be held off until a particular function definition was