返回值语法和模板不工作 [英] return value syntax and template not working

查看:174
本文介绍了返回值语法和模板不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么此代码失败,


错误C2893:无法专门化函数模板未知类型
makeAndProcessObject (const Builder&)'

error C2893: Failed to specialize function template ''unknown-type' makeAndProcessObject(const Builder &)'

我正在使用MSVC2012

I'm using MSVC2012

class BBuilder
{
public:
    int makeObject()
    {
        return 22;
    }
};

template <typename Builder>
auto
makeAndProcessObject (const Builder& builder) -> decltype( builder.makeObject() )
{
    auto val = builder.makeObject();
    // do stuff with val
    return val;
}

int main()
{
    BBuilder myobj;
    auto retval = makeAndProcessObject(myobj);

    return 0;
}

(Live example

推荐答案

makeObject 应该是const,因为你试图在常量对象上调用此函数,然后全部工作。 示例

You function makeObject should be const, since you are trying to call this function on constant object, and then all works. example

这篇关于返回值语法和模板不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆