如果函数的地址在扣除过程中无法解析,是SFINAE还是编译器错误? [英] If the address of a function can not be resolved during deduction, is it SFINAE or a compiler error?

查看:143
本文介绍了如果函数的地址在扣除过程中无法解析,是SFINAE还是编译器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 0x中,SFINAE规则已经被简化,使得在扣除的直接上下文中出现的任何无效表达式或类型不会导致编译器错误,而是导致失败(SFINAE)。

In C++0x SFINAE rules have been simplified such that any invalid expression or type that occurs in the "immediate context" of deduction does not result in a compiler error but rather in deduction failure (SFINAE).

我的问题是这样:

如果我获取一个重载函数的地址,并且无法解决,上下文的扣除?

(即是否是硬错误或SFINAE,如果无法解决?)

My question is this:
If I take the address of an overloaded function and it can not be resolved, is that failure in the immediate-context of deduction?
(i.e is it a hard error or SFINAE if it can not be resolved)?

这里是一些示例代码: / p>

Here is some sample code:

struct X
{
  // template<class T> T* foo(T,T); // lets not over-complicate things for now
  void foo(char);
  void foo(int);
};


template<class U> struct S
{
  template<int> struct size_map 
  { typedef int type; };


// here is where we take the address of a possibly overloaded function
  template<class T> void f(T, 
      typename size_map<sizeof(&U::foo)>::type* = 0); 


  void f(...);
};

int main()
{
  S<X> s;

// should this cause a compiler error because 'auto T = &X::foo' is invalid?
  s.f(3);  

}

Gcc 4.5声明这是一个编译器错误,

Gcc 4.5 states that this is a compiler error, and clang spits out an assertion violation.

以下是一些更感兴趣的相关问题:

Here are some more related questions of interest:

FCD-C ++ 0x清楚地指明了这里应该发生什么?

编译器在拒绝这个代码时是否错了?

immediate-context的扣除需要定义好一点吗?

Does the FCD-C++0x clearly specify what should happen here?
Are the compilers wrong in rejecting this code?
Does the "immediate-context" of deduction need to be defined a little better?

谢谢!

推荐答案

template<class T> void f(T, 
    typename size_map<sizeof(&U::foo)>::type* = 0); 

这不起作用,因为 U 不参与扣除。 U 是一个依赖类型,在扣除 f 时,它被视为一个固定类型,拼写为非独立名称。您需要将其添加到 f

This doesn't work, because U does not participate in deduction. While U is a dependent type, during deduction for f it's treated like a fixed type spelled with a nondependent name. You need to add it to the parameter list of f

/* fortunately, default arguments are allowed for 
 * function templates by C++0x */
template<class T, class U1 = U> void f(T, 
    typename size_map<sizeof(&U1::foo)>::type* = 0); 

所以在你的情况下,因为 U :: foo 不依赖于 f 本身的参数,您会在隐式实例化 S< X> 注释掉调用,它应该仍然失败)。 FCD在上说:14.7.1 / 1

So in your case because U::foo does not depend on parameters of f itself, you receive an error while implicitly instantiating S<X> (try to comment out the call, and it should still fail). The FCD says at 14.7.1/1


类的隐式实例化模板专用化导致类成员函数,成员类,静态数据成员和成员模板的声明的隐式实例化,而不是定义或默认参数;

The implicit instantiation of a class template specialization causes the implicit instantiation of the declarations, but not of the definitions or default arguments, of the class member functions, member classes, static data members and member templates;

也就是说,如果你隐式实例化 S< X> ,下面的函数模板声明将被实例化

That is, if you implicitly instantiate S<X> the following function template declaration will be instantiated

template<class T> void S<X>::f(T, 
  typename size_map<sizeof(&X::foo)>::type* = 0); 

然后,对该模板声明的分析将发现它无法解析 X :: foo 和错误输出。如果添加 U1 ,模板声明将不会尝试解析对 U1 :: foo 的引用(因为 U1 f 的参数,因此将保持有效,并且 f 试图被调用。

Analysis on that template declaration will then find that it can't resolve the reference to X::foo and error out. If you add U1, the template declaration will not yet try to resolve the reference to U1::foo (since U1 is a parameter of f), and will thus remain valid and SFINAE when f is tried to be called.

这篇关于如果函数的地址在扣除过程中无法解析,是SFINAE还是编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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