将其作为模板参数传递
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;
}
在上面的每个例子中,函数模板都是在编译时推导出来的,这样编译器就可以正确地生成必要的代码。与此模板一起使用的类应该具有适当的可复制性和可复制构造性。
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用指向成员的指针将成员函数作为参数传递
- 如何将参数传递给正在使用模板的类
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将部分流作为参数传递
- 我正在开发服务器,ip作为参数传递不起作用
- 将成员函数指针作为参数传递给模板方法
- 如何在C++中将迭代器作为函数参数传递
- 将附加参数传递给使用 beast::bind_front_handler 调用的函数
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 将参数传递为"const"的奇怪效果
- 如何在 c++ 中将函数作为参数传递?
- 在 C++ 中将非指定类型作为参数传递的最佳方法?
- 使用引用与指针将数组作为参数传递
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
- 为什么我不能将引用作为 std::async 的函数参数传递
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 将__device__ lambda 作为参数传递给 __global__ 函数