繁体   English   中英

将此作为模板参数传递

[英]passing this as template argument

我有两个类test1和test2:

struct test1
{
   int r;

   test1() {}
   test1(int value) : r(value) {}

   test1 foo() const;
};

struct test2
{
   int r;

   test2() {}
   test2(int value) : r(value) {}
};

template <typename t>
struct data_t
{
   static const t one;
};

template <> const test1 data_t<test1>::one  = test1(1);
template <> const test2 data_t<test2>::one  = test2(1);

然后我创建了一个函数来做某事:

template <typename t, const t& value>
t do_somthing()
{ return value; };

do_something的动作很简单,它返回一个值的副本,所以在main函数中:

int main()
{
   test1 r = do_somthing<test1, data_t<test1>::one>();
}

实现test1 :: foo时会出现问题

test1 test1::foo() const
{ return do_somthing<test1, *this>(); }

编译器因错误而停止: 'this' : can only be referenced inside non-static member functions

使用*this将成为test1 const&可接受作为第二个参数,那么为什么会出现这个错误?

当您调用template方法时明确提及参数,如

do_somthing<test1, data_t<test1>::one>(); //(1) ok
do_somthing<test1, *this>();  // (2) error

然后编译器期望显式参数应该是编译时常量。 在你的(2)nd情况下, *this不能解析为编译时常量 ,因此你得到编译器错误。

将定义更改为:

template <typename t>
t do_somthing(const t& value)
{ return value; };

现在当你打电话给,

do_somthing<test1>(*this);  // (2) ok

它应该工作。 因为,现在const t&不需要是编译时常量,即使它在编译时解析了。

编译器告诉你为什么这不起作用, 'this' : can only be referenced inside non-static member functions 它不适用于任何其他环境。

如果要以这种方式模板化此函数,则必须使用能够在编译时推导出参数类型的模板函数,如下所示:

template <class T>
T copy_value(const T& value)
{
    return value;
}

class A
{
public:
    A clone()
    {
        return copy_value(*this);
    }
};

int main()
{
    int x = 999;
    int y = copy_value(x);

    double d = 9.99;
    double e = copy_value(d);

    std::string s = "Test";
    std::string t = copy_value(s);

    return 0;
}

在上面的每个例子中,函数模板在编译时推导出来,因此编译器可以正确地生成必要的代码。 您使用此模板的类应该是可以复制的,并且可以复制构造。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM