指向一个模板函数的不同实例的指针保证比较不等式? [英] Pointers to different instances of one templated function guaranteed to compare unequal?

查看:124
本文介绍了指向一个模板函数的不同实例的指针保证比较不等式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以假设两个函数指针,指向一个模板函数的不同实例,将比较不等式?
即使模板函数根本不使用模板参数,因此在每种情况下都做同样的事情?

Is it safe to assume that two function pointers, that point to different instances of one templated function, will compare unequal? Even if the templated function does not use the template parameter(s) at all and thus does the exact same thing in every case?

例如下面的对我的编译器工作正常,但我不知道如果它在任何其他:

For example the following works fine on my compiler but I'm not sure if it does on any other:

class TypeChecker
{
public:
    template< typename T > static void foo( void )
    {}
    template< typename T > static void setType( void )
    { s_fooPtr = &foo< T >; }
    template< typename T > static bool checkType( void )
    { return ( s_fooPtr == &foo< T > ); }
private:
    static void ( * s_fooPtr )( void );
};

void ( * TypeChecker::s_fooPtr )( void ) = 0;

int main( void )
{
    TypeChecker::setType< char >();
    TypeChecker::checkType< char >();           // true
    TypeChecker::checkType< unsigned char >();  // false
    TypeChecker::checkType< signed char >();    // false
}


推荐答案

两个指针何时会相等?

根据5.10 / 1:

According to 5.10/1:


==(等于)和!=(不等于)运算符具有与关系
运算符相同的
语义限制,转换和结果类型,除了它们的较低优先级和真值结果。 [
注意: a a< b
== c< d
>和 c 具有相同的真值。 - end note]相同类型的指针(指针转换后)可以比较
的相等性。如果
和两个指针都为空,两个指向相同的函数
都代表相同的地址(3.9.2) 。

The == (equal to) and the != (not equal to) operators have the same semantic restrictions, conversions, and result type as the relational operators except for their lower precedence and truth-value result. [ Note: a<b == c<d is true whenever a<b and c<d have the same truth-value. —end note ] Pointers of the same type (after pointer conversions) can be compared for equality. Two pointers of the same type compare equal if and only if they are both null, both point to the same function, or both represent the same address (3.9.2).

foo< int>() foo< char>()相同的函数?

Are foo<int>() and foo<char>() the same function?

根据14.4 /

According to 14.4/1:


如果

Two template-ids refer to the same class or function if


    两个模板标识指同一类或函数
  • 其模板名称,操作员函数标识或字面值操作符标识指向相同的模板,

  • 其对应类型模板参数的类型相同

  • 其整数或枚举类型的相应非类型模板参数具有相同的值

  • 类型指针类型的模板参数指的是相同的外部对象或
    函数,或者都是空指针值和

  • 它们的相应的非类型模板参数指针 - 成员类型指的是相同的类
    成员或都是空成员指针值和

  • 它们对应的非类型模板参数引用类型引用相同的外部对象
    或函数和

  • 其相应的模板模板参数指向相同的模板。

  • their template-names, operator-function-ids, or literal-operator-ids refer to the same template and
  • their corresponding type template-arguments are the same type and
  • their corresponding non-type template arguments of integral or enumeration type have identical values and
  • their corresponding non-type template-arguments of pointer type refer to the same external object or function or are both the null pointer value and
  • their corresponding non-type template-arguments of pointer-to-member type refer to the same class member or are both the null member pointer value and
  • their corresponding non-type template-arguments of reference type refer to the same external object or function and
  • their corresponding template template-arguments refer to the same template.

显然 foo< int>() foo< char>()不是相同的函数。

So apparently foo<int>() and foo<char>() are not the same function.

因此& foo< int>()& foo< char>()不应比较等于,无论进行何种优化。

So &foo<int>() and &foo<char>() should not compare equal, whatever optimization is made.

编辑:

如@SergeDundich在评论中所述,14.4 / 1使用 if 而不是当且仅当 c>时,才能保证 foo< int> c $ c>和 foo< char>()是否是相同的函数。在规范的其他部分,当且仅当被使用了很多。

As mentioned by @SergeDundich in the comment, 14.4/1 used if instead if and only if, which gives no guarantee whether foo<int>() and foo<char>() are the same function or not. In other parts of the specification, if and only if is used a lot.

在说明书中对此进行澄清。但是,在示例中,我可以找到这个:

I didn't find any clarification to this in the specification. However, in the examples, I can find this:


template<class T, void(*err_fct)()> class list { /* ... */ };
list<int,&error_handler1> x1;
list<int,&error_handler2> x2;
list<int,&error_handler2> x3;
list<char,&error_handler2> x4;

声明x2和x3为相同类型。

declares x2 and x3 to be of the same type. Their type differs from the types of x1 and x4.






EDIT2:

如果使用 c $ c>当且仅当存在时(例如来自14.5.7 / 2)

if is used instead of if and only if as this situation exists: (Example from 14.5.7/2)

template<class T> struct Alloc { /* ... */ };
template<class T> using Vec = vector<T, Alloc<T>>;
Vec<int> v; // same as vector<int, Alloc<int>> v;

Vec< int> vector< int,Alloc< int>> 有很多差异,但仍然是相同的类型。

Vec<int> and vector<int, Alloc<int>> have a lot of differences, but still the same type.

对于 foo< int>() foo< char>()的情况,它们的签名是不同的。不同的签名应该使它们具有不同的功能。

However, as to the case of foo<int>() and foo<char>(), their signatures are different. The different signatures should render them different functions.

感谢@ JohannesSchaub-litb。

Thanks to @JohannesSchaub-litb .

这篇关于指向一个模板函数的不同实例的指针保证比较不等式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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