扣除模板参数C ++ [英] Deduction template argument C++

查看:129
本文介绍了扣除模板参数C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

 模板< typename T& 
bool function1(T some_var){return true; }

template< typename T>
bool(* function2())(T){
return function1< T>
}

void function3(bool(* input_function)(char)){}


b $ b

如果我调用

  function3(function2< char>()); 

没问题。但如果我调用

  function3(function2()); 

编译器给出的错误是它无法推导模板的参数。



你能建议(给出一个想法)如何重写function1和/或function2(可能基本上使用类重写),以确保吗?



*添加*



我试图在Boost.LambdaLib中做一些简单的事情, be,我的方法错误):

  sort(some_vector.begin(),some_vector.end(),_1< ; _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>一个;
}

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

它的工作原理:

  sort(a,a + 5,m_1.operator< int>(m_2)); 

但我想它不需要像LambdaLib中的模板参数。

解决方案

从返回类型中扣除是不可能的。因此, function2 不能从你期望的返回类型中推导出来。



所以你可以用一个帮助结构替换 function2 如:不幸的是没有标准的语法声明转换函数指针没有typedef和类型扣除通过typedef工作。下面的定义在一些编译器中工作(在G ++ 4.5中工作,在VC ++ 9中不起作用):

  struct function2 {
模板< typename T>
(* operator bool())(T){
return function1< T>
}
};

(另请参阅 C ++用于转换为函数指针的转换运算符)。



看起来是一样的。



注意:C ++ 11引入了可以模板化的替代typedef语法。它将是:

  struct function2 {
template< typename T&
using ftype = bool(*)(T);

template< typename T>
operator ftype< T>(){
return function1< T>
}
};

但我没有G ++ 4.7或VC ++ 10,所以我不能测试





广告已添加:

Boost.Lambda的技巧是它不返回函数,而是函数。函子可以是类模板。所以你会有:

  template< typename T> 
bool function1(T some_var){return true; }

class function2 {
template< typename T>
bool operator()(T t){
function1< T>
}
};

template< typename F>
void function3(F input_function){... input_function(something)...}

现在您可以写:

  function3(function2); 

,它会解析 function3 。所有STL都使用函数作为模板,所以它将使用所有的STL。



但是如果不想有 function3 作为模板,还有一种方法。与函数指针不同, std :: function (仅限C ++ 11,对于旧编译器使用 boost :: function )模板可以从任何函子(其包括简单函数指针)构造。所以给定上面的代码,你可以这样写:

  void function3(std :: function< bool ){... input_function(something)...} 

现在你仍然可以调用: / p>

  function3(function2()); 

关键是 std :: function 有一个模板构造函数,它在内部生成一个模板包装并存储一个指向它的方法的指针,这是不需要其他模板可调用的。


Please, consider the code below:

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) ) {}

If I call

function3(function2<char>());

it is ok. But if I call

function3(function2());

compiler gives the error that it is not able to deduction the argument for template.

Could you, please, advise (give an idea) how to rewrite function1 and/or function2 (may be, fundamentally to rewrite using classes) to make it ok?

* Added *

I am trying to do something simple like lambda expressions in Boost.LambdaLib (may be, I am on a wrong way):

sort(some_vector.begin(), some_vector.end(), _1 < _2)

I did this:

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

It works:

sort(a, a + 5, m_1.operator< <int>(m_2));

But I want that it doesn't require template argument as in LambdaLib.

解决方案

Deduction from return type is not possible. So function2 can't be deduced from what return type you expect.

It is however possible to deduce cast operator. So you can replace function2 with a helper structure like: Unfortunately there is no standard syntax for declaring cast operator to function pointer without typedef and type deduction won't work through typedef. Following definition works in some compilers (works in G++ 4.5, does not work in VC++ 9):

struct function2 {
    template <typename T>
    (*operator bool())(T) {
        return function1<T>;
    }
};

(see also C++ Conversion operator for converting to function pointer).

The call should than still look the same.

Note: C++11 introduces alternative typedef syntax which can be templated. It would be like:

struct function2 {
    template <typename T>
    using ftype = bool(*)(T);

    template <typename T>
    operator ftype<T>() {
        return function1<T>;
    }
};

but I have neither G++ 4.7 nor VC++ 10 at hand, so I can't test whether it actually works.


Ad Added:

The trick in Boost.Lambda is that it does not return functions, but functors. And functors can be class templates. So you'd have:

template<typename T>
bool function1(T some_var) { return true; }

class function2 {
    template <typename T>
    bool operator()(T t) {
        function1<T>;
    }
};

template <typename F>
void function3( F input_function ) { ... input_function(something) ... }

Now you can write:

function3(function2);

and it's going to resolve the template inside function3. All STL takes functors as templates, so that's going to work with all STL.

However if don't want to have function3 as a template, there is still a way. Unlike function pointer, the std::function (C++11 only, use boost::function for older compilers) template can be constructed from any functor (which includes plain function pointers). So given the above, you can write:

void function3(std::function<bool ()(char)> input_function) { ... input_function(something) ... }

and now you can still call:

function3(function2());

The point is that std::function has a template constructor that internally generates a template wrapper and stores a pointer to it's method, which is than callable without further templates.

这篇关于扣除模板参数C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆