#include <type_traits>
auto call(const auto& f) -> typename std::result_of<decltype(f)()>::type
{
  return f();
}
int main()
{
  return call([] { return 0; });
}
Neither gcc-4.9.2 and gcc-5.0.0 compil!!!!
原因: result_of needs a call expression from which it will deduce the return type,
const auto& f中的f会被初始化为函数签名原型,而非函数引用.
ok version:
template<typename F>
auto call(F const& f) -> typename std::result_of<decltype(f)()>::type
//                    or typename std::result_of<F()>::type
{
  return f();
}
或者
template<typename F>
auto call(F const& f) -> decltype(f())
{
  return f();
}
Anyway, on gcc 4.5, result_of is implemented in terms of decltype:
template<typename _Signature>
  class result_of;
template<typename _Functor, typename... _ArgTypes>
  struct result_of<_Functor(_ArgTypes...)>
  {
    typedef decltype( std::declval<_Functor>()(std::declval<_ArgTypes>()...) )  type;
  };
  
  
template <class F, class R = result_of_t<F()>>
R call(F& f) { return f(); }
int answer() { return 42; }
call(answer); // error
使用:result_of_t<F&()>
/**************************正确的方式************************************************************/ 
#include <iostream>
#include <type_traits>
double f(int i)
{
        return i+0.1;
}
struct F
{
        public:
        double operator ()(int i) { return i+0.1; }
};
int
main(int, char**)
{
        std::result_of<F(int)>::type x;     
        std::result_of<f(int)>::type x;  //错误的用法
//typename std::result_of<decltype(&f)(int)>::type x; //正确的方式1
        //typename std::result_of<decltype((f))(int)>::type x; //正确的方式2, decltype((f))会返回一个引用
        x = 0.1;
        std::cerr << x << std::endl;
}
struct AA {};
int foo(AA x)
{
    return 0;
}
template<typename T>
void Test(const T& t)
{
    decltype(std::declval<T>()(std::declval<AA>())) i1 = 1;
    typename std::result_of<decltype(&t)(AA)>::type i2 = 2; //正确的方式1

typename std::result_of<decltype(t)(AA)>::type i2 = 2; //正确的方式2

    typename std::result_of<const T &(AA)>::type i3 = 2; //正确的方式3
}
int main()
{
    Test(foo);
    return 0;
}

g++ tpl.cpp -std=c++11 -g -fno-elide-constructors -O0

std result_of的更多相关文章

  1. 【error】no type named ‘type’ in ‘class std::result_of<void

    Q: std::thread fs_module(fs_process, prob_orig, fb_sz, line_num, probp, plabel, std::ref(confidence_ ...

  2. /usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void

    /usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std ...

  3. C++11:使用 auto/decltype/result_of使代码可读易维护

    C++11 终于加入了自动类型推导.以前,我们不得不使用Boost的相关组件来实现,现在,我们可以使用"原生态"的自动类型推导了! C++引入自动的类型推导,并不是在向动态语言(强 ...

  4. C++笔记--std::相关

    std::packaged_task https://www.cnblogs.com/haippy/p/3279565.html https://en.cppreference.com/w/cpp/t ...

  5. C++ std::thread

    std::thread Defined in header class thread The class thread represents a single thread of execution. ...

  6. 第30课 线程同步(std::condition_variable)

    一. 条件变量 (一)条件变量概述 多线程访问一个共享资源(或称临界区),不仅需要用互斥锁实现独享访问避免并发错误,在获得互斥锁进入临界区后,还需检查特定条件是否成立.当某个线程修改测试条件后,将通知 ...

  7. std::invoke_result的实现详解

    目录 目录 前言 invoke_result 标准库中的invoke_result 我的实现 后记 前言 本篇博文将详细介绍一下libstdc++中std::invoke_result的实现过程,由于 ...

  8. C++11的简单线程池代码阅读

    这是一个简单的C++11实现的线程池,代码很简单. 原理就是管理一个任务队列和一个工作线程队列. 工作线程不断的从任务队列取任务,然后执行.如果没有任务就等待新任务的到来.添加新任务的时候先添加到任务 ...

  9. c++新特性与boost

    <Boost程序库探秘——深度解析C++准标准库>之试读 前一阵子还看到一篇文章,说C#要重蹈C++的覆辙,这里说的C++的覆辙是什么呢?是指C++语言过于臃肿的功能特性,导致学习人员的流 ...

随机推荐

  1. nginx https使用

    默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖于OpenSSL库和一些引用文件,通常这些文件并不在同一个软件包中.通常这 ...

  2. IT公司100题-tencent-打印所有高度为2的路径

    问题描述: 打印所有到叶子节点长度为2的路径  10  /  \ 6   16 / \   / \ 4 8  14 18   / \    / \    \ 2  5  12 15 20 / 11   ...

  3. Swift语言—有趣的字符串连接、数组、字典

    字符串链接:Swift语言中的字符串连接方式本人觉得非常的有趣,变量连接需要用右斜杠,并且变量名要括起来 “\(变量名)”,后面的字符串连接分别用逗号 ‘ , ’ 隔开 数组: Var arr = [ ...

  4. MVC乱码可能的原因

    1.数据传输不对,或者根本没有进控制器 2.分部视图建立时一定要选择

  5. 安卓手机上的python运行环境-qpython

    qpython是一个能让安卓手机运行和编写python的APP,到网上下载APK安装或者在GOOGLE PLAY搜索安装即可. 安装之后你可以你手机跑自己的python程序. qpython有两个大版 ...

  6. jQueryMobile控件之ListView

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. C# .net windows服务启动多个服务 ServiceBase

    在windows服务中想要启动多个服务 ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { // new SyncServ ...

  8. CBC和CTR解密模式——C++实现

    利用已经封装好的AES加密算法,实现CBC模式加密和CTR模式加密. (1)CBC解密 如图,CBC模式的解密,步骤主要有三个,首先是拿密文段逐一放到AES解密盒子里面得到一个结果temp(事先要把密 ...

  9. node.js学习笔记【1】

    http://howtonode.org/how-to-install-nodejs http://www.mcclean-cooper.com/valentino/cygwin_install/

  10. CentOS上的RabbitMQ安装

    1. erlang安装配置(这里我们在opt目录下进行安装配置) cd /opt 安装依赖文件: yum install gcc glibc-devel make ncurses-devel open ...