是否可以获得内置标准运算符的函数指针? [英] Is it possible to get the function pointer of a built-in standard operator?

查看:130
本文介绍了是否可以获得内置标准运算符的函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想引用内置运算符的函数指针,但我不知道如何指定特定的类型重载。



我有以下模板类签名:

 模板< typename ParamsType,typename FnCompareType> 
class MyAction
{
public:
MyAction(ParamsType& arg0,ParamsType& arg1,FnCompareType& fnCpmpare)
:arg0_(arg0),arg1_ fnCompare_(fnCpmpare){}

bool operator()
{
if((* fnCompare _)(arg0_,arg1_)
{
/ / do this
}
else
{
// do s.th. else
}
}

private:
ParamsType& arg0_;
ParamsType& arg1_;
FnCompareType& fnCompare_;
}

并且想要使用这样的语法:

  void doConditional(int param1,int param2) 
{
MyAction< int,& :: operator> => action(param1,param2);
if(action())
{
//执行此操作
}
else
{
//执行
}
}

但这不会编译:

 错误:运算符> ='尚未声明

我可以做什么来引用这样的内部静态操作? / p>

解决方案

内置运算符



为什么不能有函数指针他们:



C ++ 11,§13.6/ 1,[over.built]




在本子句中指定了代表第5条中定义的内置运算符的候选运算符函数。这些候选函数参与13.3.1.2 中描述的并且不用于其他用途的操作符重载解析过程。


内建运算符(内建类型的运算符)不是真实的运算符函数 。所以你不能有指向它们的函数指针。您也不能使用 operator<(A,B)语法调用它们。
它们只参与重载解析,但编译器会将它们直接转换成适当的asm /机器指令,而不用任何类型的函数调用。



解决此问题:



user1034749已回答此问题,但为了完整性:



标准在§20.8中定义了很多函数对象[function.objects]


  • 算术运算

  • 比较

  • 逻辑运算

  • 位运算




函数对象是函数对象类型的对象。在期望将函数的指针传递到算法模板的地方(第25条),指定接口接受函数对象。这不仅使算法模板与函数的指针一起工作,而且使它们能够处理任意函数对象。




C ++ 11 ,§20.8.5,[comparisons]





  • equal_to

  • not_equal_to

  • 更大,更少

  • greater_equal

  • less_equal


这些是模板函数对象,它们在 operator()函数中衰减到类似的运算符。它们可以用作函数指针参数。



user1034749是对的,我想说明:没有其他方法,



标准类类型运算符



您可以使用标准库操作符作为函数指针(作为真实函数存在)。



但是你必须引用模板的相应实例。编译器将需要适当的提示来推导正确的模板。



这对我来说在MSVC 2012上使用 operator + std :: basic_string

  template< class Test> 
Test test_function(Test const& a,Test const& b,Test(* FPtr)(Test const& Test const&))
{
return FPtr b);
}

int main(int argc,char * argv [])
{
typedef std :: char_traits< char> traits_t;
typedef std :: allocator< char> alloc_t;
std :: basic_string< char,traits_t,alloc_t> a(test),b(test2);
std :: cout<< test_function< std :: basic_string< char,traits_t,alloc_t>>(a,b,& std :: operator +)< std :: endl;
return 0;
}

如果 test_function 被遗漏,将被推断,这将失败(至少对于MSVC 2012)。


I want to refer to function pointers of built-in operators, but I don't know how to specify the specific type overloads.

I have the following template class signature:

template<typename ParamsType, typename FnCompareType>
class MyAction
{
public:
    MyAction(ParamsType& arg0, ParamsType& arg1, FnCompareType& fnCpmpare) 
    : arg0_(arg0), arg1_(arg1), fnCompare_(fnCpmpare) {}

    bool operator()()
    {
        if((*fnCompare_)(arg0_,arg1_)
        {
            // do this
        }
        else
        {
            // do s.th. else
        }
    }

private:
    ParamsType& arg0_;
    ParamsType& arg1_;
    FnCompareType& fnCompare_;
}

And want to use a syntax like this:

void doConditional(int param1, int param2)
{
    MyAction<int,&::operator>=> action(param1,param2);
    if(action())
    {
        // Do this
    }
    else
    {
        // Do that
    }
}

But that doesn't compile:

error: ‘::operator>=’ has not been declared

What can I do to refer to such intrinsic static operations?

解决方案

Built-in operators

Why you cannot have function pointers of them:

C++11, §13.6/1, [over.built]

The candidate operator functions that represent the built-in operators defined in Clause 5 are specified in this subclause. These candidate functions participate in the operator overload resolution process as described in 13.3.1.2 and are used for no other purpose.

Built-in operators (those for the built-in types) aren't real operator functions. So you can't have function pointer pointing to them. You also cannot invoke them using operator<(A,B) syntax. They only participate in overload resolution but the compiler will translate them directly into the appropriate asm/machine instruction without any kind of "function call".

The way to get around this issue:

user1034749 has already answered this question, but for completeness:

The standard defines a lot of function objects in §20.8, [function.objects], i.e.

  • Arithmetic operations
  • Comparisons
  • Logic operations
  • Bitwise operations

A function object is an object of a function object type. In the places where one would expect to pass a pointer to a function to an algorithmic template (Clause 25), the interface is specified to accept a function object. This not only makes algorithmic templates work with pointers to functions, but also enables them to work with arbitrary function objects.

C++11, §20.8.5, [comparisons]

  • equal_to
  • not_equal_to
  • greater, less
  • greater_equal
  • less_equal

Those are templated function objects which decay to the analogous operator in their operator() function. They can be used as function pointer arguments.

user1034749 is right, I want to state: There's no other way, these are completely equivalent in usage to 'raw' function pointers. Reference given.

Standard class type operators

You can use standard library operators as function pointers (which are present as "real functions").

But you'll have to refer to the respective instance of the template. The compiler will need appropriate hints to deduce the correct template.

This works for me on MSVC 2012 using operator+ of std::basic_string

template<class Test>
Test test_function (Test const &a, Test const &b, Test (*FPtr)(Test const &, Test const &))
{
   return FPtr(a, b);
}

int main(int argc, char* argv[])
{
   typedef std::char_traits<char> traits_t;
   typedef std::allocator<char> alloc_t;
   std::basic_string<char, traits_t, alloc_t> a("test"), b("test2");
   std::cout << test_function<std::basic_string<char, traits_t, alloc_t>>(a, b, &std::operator+) << std::endl;
   return 0;
}

If the template argument of test_function is left out to be deduced this will fail (at least for MSVC 2012).

这篇关于是否可以获得内置标准运算符的函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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