在函数调用时避免模板规范
Avoiding template specification at function call
本文关键字:函数调用 更新时间:2023-10-16
我使用模板,我讨厌用模板参数调用函数。例子:
我有一个模板结构
template<typename T>
struct telement {
typedef {
T element;
float some_value;
} type;
};
telement<float>::type theElement;
和我有一个函数:
template<typename T>
float do_some_stuff( const typename telement<T>::type& x) {
// ...
}
不幸的是,当我调用
时,g++会报错。do_some_stuff( theElement );
and I must call:
do_some_stuff<float>( theElement );
每次调用函数都可以避免模板规范吗?我认为编译器应该自动找出类型…
这是不可演绎的上下文之一。编译器不能推导出模板参数T
。
问自己这个问题:当你写这篇文章的时候,
do_some_stuff( theElement );
你认为T
应该推断什么类型的参数?
你可能认为是T=float
?嗯,这只是一种可能性。可以存在类模板的专门化,如:
//the compiler cannot assume that such specialization doesn't exist!
template<>
struct telement<someType>
{
typedef {
float element;
float some_value;
} type;
};
即使在这种情况下,嵌套类型telement<someType>::type
与telement<float>::type
相同。你看到模棱两可了吗?T
和嵌套的type
之间没有一一对应的关系。事实上可能存在多重关系。可以有许多T
,它们的嵌套类型是相同的。
那么给定嵌套类型,编译器如何决定模板参数?它不能
已经有几个关于SO的主题解释了类似的情况,参见这些:
- c++,模板参数不能推导
- 专业iterator_traits
解决方案是,只需将函数模板编写为:
template<typename T>
float do_some_stuff( const T& x) {
// ...
}
毕竟,在形参中写telement<T>::type
有什么意义呢?
这是一种使用它的方法:
#include <iostream>
template<typename T>
struct telement {
typedef struct {
typedef T t_element_type;
typedef float t_value_type;
t_element_type element;
t_value_type some_value;
} type;
};
template<typename T>
float imp_do_some_stuff(const typename telement<T>::type& x) {
return x.some_value;
}
template<typename T>
float do_some_stuff(const T& x) {
return imp_do_some_stuff<typename T::t_element_type>(x);
}
int main() {
telement<float>::type theElement;
do_some_stuff(theElement);
return 0;
}
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销