c++ - 指向一个模板化函数的不同实例的指针保证比较不相等?

标签 c++ templates function-pointers

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

例如,以下在我的编译器上运行良好,但我不确定它是否适用于任何其他:

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:

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>()同样的功能?

根据 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>()功能不一样。

所以 &foo<int>()&foo<char>()不应该比较相等,无论做了什么优化。


编辑:

正如@SergeDundich 在评论中提到的,14.4/1 使用了 if而是 if and only if ,它给出 no 保证是否 foo<int>()foo<char>()是否相同的功能。在规范的其他部分,if and only if经常使用。

我没有在规范中找到对此的任何说明。但是,在示例中,我可以找到:

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;

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


EDIT2:

if用于代替 if and only if因为这种情况存在:(示例来自 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>>有很多不同,但仍然是同一类型。

但是,对于 foo<int>() 的情况和 foo<char>() ,他们的签名是不同的。不同的签名应该赋予它们不同的功能。

感谢@JohannesSchaub-litb。

关于c++ - 指向一个模板化函数的不同实例的指针保证比较不相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8667515/

相关文章:

c++ - STL 按变量排序,然后按升序排序

c++ - OpenCV FREAK 返回太多异常值

c++ - 模板的使用

C++:如何专门针对左值和右值

c - C 中的函数数组

c++ - 纹理未通过 glGenerateMipmap() 加载

C++11 多线程在神经网络中的性能非常低

c++ - 如果模板类型本身就是模板,如何获取迭代器?

c - 如何重新分配函数指针数组?

c - C 中指向函数 (**ppf)() 的指针与指向函数 (*pf)() 的指针有何不同?