#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. mybitis学习的页面

    http://mybatis.github.io/mybatis-3/zh/configuration.html

  2. [转]如何让div中的内容垂直居中

    转自:http://blog.163.com/yan_1990/blog/static/197805107201211311515454/ 虽然Div布局已经基本上取代了表格布局,但表格布局和Div布 ...

  3. PHP EMS: 开源 在线考试系统安装

    PHPEMS: 在线考试系统调测记录 下载安装软件包 PE2014.RAR 环境要求:利用了RHEL 5.X的一个环境,系统要求的运行环境是PHP 5.2以上,MYSQL 5.0以上.看了一下光盘,发 ...

  4. 如何使用 WinInet 时提供下载上载进度信息

    概要许多开发人员都使用 WinInet 函数来下载或上载文件在 Internet 上的想要提供一个进度条以指示多少文件传输已完成,但多少就越长.您可以使用以下机制来完成此.Collapse image ...

  5. Android TextView里显示两种颜色

    今天介绍一个小技巧,在Android的TextView里设置两种颜色,直接上代码: TextView TV = (TextView)findViewById(R.id.mytextview01); S ...

  6. 【转】nginx+tomcat+memcached (msm)实现 session同步复制

    出现session不同步时,请放到content.xml中,实际验证有效: tomcat + memcached + nginx 实现session共享 这里重点强调如何实现linux服务器上 服务器 ...

  7. Hinet 日本数据处理流程

    ---恢复内容开始--- 推荐网站: http://ju.outofmemory.cn/entry/138571 ridnet.py 将Hinet 的cnt 数据提取为sac数据,参考网站 http: ...

  8. python多线程监控指定目录

    import win32file import tempfile import threading import win32con import os dirs=["C:\\WINDOWS\ ...

  9. php大力力 [040节] 买了一天域名,整了一天后台,新网后台不懂啊

    php大力力 [040节] 买了一天域名,整了一天后台,新网后台不懂啊]]] 还有万网那些域名要备案,备案,备案中...................wqnmlgb 今天摩托车的前后轮被扎了,tnn ...

  10. Linux内核分析——跟踪分析Linux内核的启动过程

    万子惠 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程 实验部分 menu程序: cd LinuxKernel/ qemu -kernel linux-3.18.6/a ...