将其作为模板参数传递

passing this as template argument

本文关键字:参数传递      更新时间:2023-10-16

我有两个类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的操作很简单,它返回value的副本,所以在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

then编译器期望显式参数应该是编译时间常数。在(2)种情况下,*this不能解析为编译时间常数,所以你得到编译器错误。

将定义更改为:

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

现在当你调用as时,

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;
}

在上面的每个例子中,函数模板都是在编译时推导出来的,这样编译器就可以正确地生成必要的代码。与此模板一起使用的类应该具有适当的可复制性和可复制构造性。