C++ return type deduction

 1 template <typename T>
 2 class TD;
 3
 4 int& foo(){
 5     static int x=0;
 6     return x;
 7 }
 8
 9 int main(){
10
11     auto lambda = []()->const auto&{ return foo(); };
12     TD<decltype(lambda())> dummy;
13 }

第11行,返回值类型按照const auto& 模式推导。这是用户指定的方式,可以随意指定成auto前后可以添加volatile const & && * **等修饰符,只要能推导成功就行。

当然,也可以指定一个具体类型:

 1 template <typename T>
 2 class TD;
 3
 4 int& foo(){
 5     static int x=0;
 6     return x;
 7 }
 8
 9 int main(){
10
11     auto lambda = []()->long{ return foo(); };
12     TD<decltype(lambda())> dummy;
13 }

第11行,强制返回值类型为long,只要foo()到long隐式转换能ok。

第三种是按返回值表达式,实现完美转发。就是按照返回值表达式的实际类型,不失真的确定函数的返回值。

template <typename T>
class TD;

int&& foo(){
    return 10;
}

int main(){

    auto lambda = []()->decltype(auto){ return foo(); };
    TD<decltype(lambda())> dummy;
}

这种方式需要decltype(auto)语法。
如果函数的返回值是void(函数体没有return语句; 或者return f()且void f();)则以下等价:

auto lambda = []()->decltype(auto){  };
auto lambda = []()->auto{  };
auto lambda = [](){  };

但是下面的不行:

auto lambda = []()->const auto{  };

对于函数中有多个出口return语句,要求各个return返回值表达式的类型必须相同。

 1 template <typename T>
 2 class TD;
 3
 4 int foo(){ return 1;}
 5 long bar(){ return 2;}
 6
 7 int main(){
 8
 9     auto lambda = []() {
10         return bar();
11         return foo();
12     };
13     TD<decltype(lambda())> dummy;
14 }

编译失败了,因为foo,bar返回值不同。推导返回值时遇到矛盾。除非具体指定一个返回值:

template <typename T>
class TD;

int foo(){ return 1;}
long bar(){ return 2;}

int main(){

    auto lambda = []()->int {
        return bar();
        return foo();
    };
    TD<decltype(lambda())> dummy;
}

如果是函数递归调用,要求函数体内第一个return语句表达式中不能有递归函数的存在。

原文地址:https://www.cnblogs.com/thomas76/p/8726895.html

时间: 2024-08-14 15:25:37

C++ return type deduction的相关文章

2-3. Using Type Deduction

Type Deduction 发生在编译时期 可以对一般类型,自定义类型进行类型自推导 下面有两个例子: 1. Using auto with a class 1 #include <iostream> 2 #include <typeinfo> 3 4 using namespace std; 5 6 class MyClass 7 { 8 }; 9 10 int main() 11 { 12 auto variable = MyClass(); 13 cout <<

现代C++之理解模板类型推断(template type deduction)

理解模板类型推断(template type deduction) 我们往往不能理解一个复杂的系统是如何运作的,但是却知道这个系统能够做什么.C++的模板类型推断便是如此,把参数传递到模板函数往往能让程序员得到满意的结果,但是却不能够比较清晰的描述其中的推断过程.模板类型推断是现代C++中被广泛使用的关键字auto的基础.当在auto上下文中使用模板类型推断的时候,它不会像应用在模板中那么直观,所以理解模板类型推断是如何在auto中运作的就很重要了. 下面将详细讨论.看下面的伪代码: templ

c语言,warning: return type of &#39;main&#39; is not `int&#39;怎么解决?

////警告可以忽略,但如果严格点的话 #include<stdio.h> #include<math.h> int main(int argc, char *arg[]) ///标准C主函数原型 {     float x,y;     printf("Enter x:");     scanf("%f",&x);     if(x<0){         y=pow(x,5)+2*x+1/x;     }     else{

【c++错误】类的语法错误 error c2533:constructors not allowed a return type(构造函数不允许返回一个类型)

今天编写类的程序的时候不小心把类后的分号忘写了,就出现上面的错误提示. 顺便复习下类的正确格式: class 类名 { public: //习惯上将公有类型放在前面,便于阅读 ……(外部接口) protected: …… (保护型成员) private: ……(私有成员) }; //这里的分号千万不能忘写,不然会出现错误error: 2533:constructors not allowed a return type

The return type is incompatible with JspSourceDependent.getDependants():JasperException问题分析与解决方法

Linux下基于JSP的报表集成到项目中后,显示不出来,查看tomcat的日志,有如下报错信息: The return type is incompatible with JspSourceDependent.getDependants() Stacktrace:] with root cause org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: [33]

Mapper method &#39;com.autoyol.mapper.trans.AccountLogMapper.getTotalIncomByMemNoLastest attempted to return null from a method with a primitive return type (int).解决方法

1.打开日志输出,降低日志级别. <AppenderRef ref="console" level="trace"></AppenderRef> 否则看不到报错.. 2.调整mysql语句,不能使用order by limit之类的 <!-- SELECT IFNULL(amt,1) FROM account_log WHERE mem_no=#{value} AND income_type != 4 ORDER BY id DESC

c++11: trailing return type in functions(函数返回类型后置)

In C++03, the return type of a function template cannot be generalized if the return type relies on those of the template arguments. Here is an example, mul(T a, T b) is a function template that calculates the product of a and b. Arguments a and b ar

instance method &#39;*****&#39; not found (return type defaults to &#39;id&#39;)

博文转载至 http://blog.csdn.net/cerastes/article/details/38025801 return type defaultsnot foundiOSwarning 在一个类里面引用另外一个类的实例方法,警告方法没有找到. 原因.h文件没有应用,只在头文件中申明了@class #import相应的.h文件就可以 instance method '*****' not found (return type defaults to 'id')

attempted to return null from a method with a primitive return type (int).

错误信息: attempted to return null from a method with a primitive return type (int). 错误原因:实际查询sql并没有这个值,出现空值,就会报这个异常. 错误sql(发生问题): select sum(id) from post where post.author = ? 正确sql(解决问题): select IFNULL(sum(id),0) from post where post.author = ? 关键在于使用