12
23
2014
3

C++11 的 decltype 一种用法

#include <string>
#include <iostream>
using namespace std;

class clsA
{
public:
    int value()
    {
        return 1;
    }
};

class clsB
{
public:
    string value()
    {
        return "yes/no";
    }
};

template<class Type>
auto foo(Type x) -> decltype(x.value())
{
    return x.value();
}

int main(void)
{
    clsA oa;
    clsB ob;
    cout << foo(oa) << foo(ob) << endl;
    return 0;
}
Category: 编程 | Tags: c++ | Read Count: 2862

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Powered by Chito | Hosted at is-Programmer | Theme: Aeros 2.0 by TheBuckmaker.com