C++ 演绎模板参数C++; 请考虑下面的代码: template<typename T> bool function1(T some_var) { return true; } template <typename T> bool (*function2())(T) { return function1<T>; } void function3( bool(*input_function)(char) ) {}
编译器给出的错误是无法推断模板的参数 请您建议(给出一个想法)如何重写function1和/或function2(基本上可能是使用类重写)以使其正常运行 *已添加* 我正在尝试做一些简单的事情,比如Boost.LambdaLib中的lambda表达式(可能是,我走错了方向):C++ 演绎模板参数C++; 请考虑下面的代码: template<typename T> bool function1(T some_var) { return true; } template <typename T> bool (*function2())(T) { return function1<T>; } void function3( bool(*input_function)(char) ) {},c++,templates,C++,Templates,编译器给出的错误是无法推断模板的参数 请您建议(给出一个想法)如何重写function1和/或function2(基本上可能是使用类重写)以使其正常运行 *已添加* 我正在尝试做一些简单的事情,比如Boost.LambdaLib中的lambda表达式(可能是,我走错了方向): 排序(一些向量.begin(),一些向量.end(),\u 1b; } 模板 bool my_func_lesser(常数T&a,常数T&b){ 返回b>a; } 我的班级比较{ 公众: int值; my_比较(int值)
排序(一些向量.begin(),一些向量.end(),\u 1<\u 2)
我这样做:
template<typename T>
bool my_func_greater (const T& a, const T& b) {
return a > b;
}
template<typename T>
bool my_func_lesser (const T& a, const T& b) {
return b > a;
}
class my_comparing {
public:
int value;
my_comparing(int value) : value(value) {}
template <typename T>
bool (*operator<(const my_comparing& another) const)(const T&, const T&) {
if (this->value == 1 && another.value == 2) {
return my_func_greater<T>;
} else {
return my_func_greater<T>;
}
}
};
const my_comparing& m_1 = my_comparing(1);
const my_comparing& m_2 = my_comparing(2);
模板
bool my_func_更大(常数T&a、常数T&b){
返回a>b;
}
模板
bool my_func_lesser(常数T&a,常数T&b){
返回b>a;
}
我的班级比较{
公众:
int值;
my_比较(int值):值(value){}
模板
bool(*operatorvalue==1&&other.value==2){
返回我的函数;
}否则{
返回我的函数;
}
}
};
const my_comparing&m_1=my_comparing(1);
const my_comparing&m_2=my_comparing(2);
它的工作原理是:
sort(a, a + 5, m_1.operator< <int>(m_2));
排序(a,a+5,m_1.运算符<(m_2));
但我希望它不像LambdaLib那样需要模板参数。编译器不使用表达式的上下文来推断其模板参数。对于编译器,
function3(function2())代码>看起来像
auto tmp = function2();
function3(tmp);
而且它不知道什么是函数2
模板参数。不可能从返回类型中扣除。因此,function2
无法从您期望的返回类型推断出来
然而,可以推导出cast运算符。因此,您可以将function2
替换为以下帮助器结构:不幸的是,在没有typedef的情况下,没有标准语法可以将cast运算符声明为函数指针,并且类型推断无法通过typedef工作。以下定义适用于某些编译器(适用于G++4.5,不适用于VC++9):
它将解析function3
中的模板。所有STL都将函子作为模板,所以这将适用于所有STL
但是,如果不想将函数3
作为模板,仍然有一种方法。与函数指针不同,std::function
(仅限C++11,对于较旧的编译器,使用boost::function
)模板可以从任何函子(包括普通函数指针)构建。鉴于上述情况,你可以写:
function3(function2);
void function3(std::function<bool ()(char)> input_function) { ... input_function(something) ... }
关键是std::function
有一个模板构造函数,它在内部生成一个模板包装器并存储指向其方法的指针,无需进一步的模板就可以调用该方法。编辑后,我认为您想做的事情可以更简单。请参见以下类型:
struct Cmp {
bool const reverse;
Cmp(bool reverse) : reverse(reverse) {}
template <typename T> bool operator()(T a, T b) {
return reverse != (a < b);
}
};
struct-Cmp{
布尔常数反转;
Cmp(布尔反转):反转(反转){}
模板布尔运算符(){
反向返回!=(a
现在,在您的操作符中,我不确定这是否有助于您,我也不是这方面的专家。我从昨天开始一直在看这篇文章,我想参与其中
模板无法推断其类型,因为编译器不知道您希望返回的类型。下面是一个与function2()类似的简单示例
是否可以将模板声明移动到类级别,如下所示:
template<typename T>
bool my_func_greater (const T& a, const T& b) {
return a > b;
}
template<typename T>
bool my_func_lesser (const T& a, const T& b) {
return b > a;
}
template <typename T>
class my_comparing {
public:
int value;
my_comparing(int value) : value(value) {}
bool (*operator<(const my_comparing& another) const)(const T&, const T&) {
if (this->value == 1 && another.value == 2) {
return my_func_greater<T>;
} else {
return my_func_greater<T>;
}
}
};
if( m_1 < m_2 )
cout << "m_1 is less than m_2" << endl;
else
cout << "m_1 is greater than m_2" << endl;
模板
bool my_func_更大(常数T&a、常数T&b){
返回a>b;
}
模板
bool my_func_lesser(常数T&a,常数T&b){
返回b>a;
}
模板
我的班级比较{
公众:
int值;
my_比较(int值):值(value){}
bool(*operatorvalue==1&&other.value==2){
返回我的函数;
}否则{
返回我的函数;
}
}
};
并声明m_1和m_2如下:
const my_comparing<int>& m_1 = my_comparing<int>(1);
const my_comparing<int>& m_2 = my_comparing<int>(2);
const my_comparing&m_1=my_comparing(1);
const my_comparing&m_2=my_comparing(2);
现在,您可以进行如下比较:
template<typename T>
bool my_func_greater (const T& a, const T& b) {
return a > b;
}
template<typename T>
bool my_func_lesser (const T& a, const T& b) {
return b > a;
}
template <typename T>
class my_comparing {
public:
int value;
my_comparing(int value) : value(value) {}
bool (*operator<(const my_comparing& another) const)(const T&, const T&) {
if (this->value == 1 && another.value == 2) {
return my_func_greater<T>;
} else {
return my_func_greater<T>;
}
}
};
if( m_1 < m_2 )
cout << "m_1 is less than m_2" << endl;
else
cout << "m_1 is greater than m_2" << endl;
if(m_1 cout类在这里没有帮助,因为模板参数推导对它们不起作用。您的问题本质上归结为基于返回类型推断参数类型,这是不可能的代码>?你能改变功能3
吗?还是那是刻在石头上的?你能解释一下功能2
的定义吗?我认为自己是一个合格的C++程序员,但我完全无法解析它。@ jalf:这是一个函数,它返回一个函数,得到一个函数< <代码> t>代码>,并返回一个<代码>布尔O/COD>。我认为这是一个标志,你确实是一个优秀的C++程序员,如果你不能立即解析它(我必须回到C天的深度来计算它)。这就是为什么我们必须为lambdas-<代码> [IN](int x){} <代码>指定参数类型,而不是<代码> [x(x){} /代码>,请你,请说明为什么此代码未在Visual Studio 2010中编译?@Programmer585:谢谢。我修正了语法,使其在gcc中正常工作,但在vc++中似乎根本不可能。
function3(function2());
struct Cmp {
bool const reverse;
Cmp(bool reverse) : reverse(reverse) {}
template <typename T> bool operator()(T a, T b) {
return reverse != (a < b);
}
};
template<typename T>
T foo() {
T t;
return t;
};
foo(); // no type specified. T cannot be deduced.
template<typename T>
bool my_func_greater (const T& a, const T& b) {
return a > b;
}
template<typename T>
bool my_func_lesser (const T& a, const T& b) {
return b > a;
}
template <typename T>
class my_comparing {
public:
int value;
my_comparing(int value) : value(value) {}
bool (*operator<(const my_comparing& another) const)(const T&, const T&) {
if (this->value == 1 && another.value == 2) {
return my_func_greater<T>;
} else {
return my_func_greater<T>;
}
}
};
const my_comparing<int>& m_1 = my_comparing<int>(1);
const my_comparing<int>& m_2 = my_comparing<int>(2);
if( m_1 < m_2 )
cout << "m_1 is less than m_2" << endl;
else
cout << "m_1 is greater than m_2" << endl;