可以将私有静态成员用作其类的成员函数的默认参数? [英] Can a private static member be used as a default argument to a member function of its class?

查看:410
本文介绍了可以将私有静态成员用作其类的成员函数的默认参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪一个编译器是正确的?

Which one of the compilers is right ?

class A
{
public:
   template <typename T>
   void fun(void (*f)() = funPrivate<T>) {}
private:
   template <typename T>
   static void funPrivate() {}
};

int main(int argc, char** argv)
{
  A a;
  a.fun<int>();
  return 0;
} 

编译良好: gcc version 4.8.5(Ubuntu 4.8。 5-2ubuntu1〜14.04.1)

导致错误: clang version 3.4-1ubuntu3(tags / RELEASE_34 / final) on LLVM 3.4)

    a.cpp:5:27: error: 'funPrivate' is a private member of 'A'
   void fun(void (*f)() = funPrivate<T>) {}
                          ^~~~~~~~~~~~~
    a.cpp:14:3: note: in instantiation of default function argument expression for 'fun<int>' required here
      a.fun<int>();
      ^
    a.cpp:8:16: note: declared private here
       static void funPrivate() {}
               ^
1 error generated.


推荐答案

§11


8默认参数(8.3.6)中的名称在声明点处是绑定的,并且在
点检查访问,而不是在任何点使用默认参数。对
函数模板和类模板的成员函数中的默认参数的访问检查按第14.7.1节中所述执行。

8 The names in a default argument (8.3.6) are bound at the point of declaration, and access is checked at that point rather than at any points of use of the default argument. Access checking for default arguments in function templates and in member functions of class templates is performed as described in 14.7.1.

§14.7.1


12如果函数模板 f 方式需要使用默认参数,查找依赖名称
,检查语义约束,并且在默认
参数中使用的任何模板的实例化,就像默认参数是在函数模板专用化中使用的初始化器
具有相同范围,相同的模板参数和与函数模板相同的访问权限 f
用于那一点。此分析称为默认参数实例化 。实例化的默认参数
然后用作 f 的参数。

12 If a function template f is called in a way that requires a default argument to be used, the dependent names are looked up, the semantics constraints are checked, and the instantiation of any template used in the default argument is done as if the default argument had been an initializer used in a function template specialization with the same scope, the same template parameters and the same access as that of the function template f used at that point. This analysis is called default argument instantiation. The instantiated default argument is then used as the argument of f.

所以,据我所知,gcc的解释是正确的。 fun 可以访问私有成员,因此其默认参数应该在同一访问中考虑。但我正在读的行之间的14.7.1(12)适用于成员模板,而不只是功能模板。也可能误会了14.7.1(12)意味着。

So, according to this, I would guess that gcc's interpretation is right. fun has access to private members, so its default arguments should be considered in that same access. But I am reading between the lines that 14.7.1(12) applies to member templates, and not just function templates. Also I may be misunderstanding that 14.7.1(12) means.

这篇关于可以将私有静态成员用作其类的成员函数的默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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