gpt4 book ai didi

c++ - 私有(private)静态成员可以用作其类的成员函数的默认参数吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:46:09 24 4
gpt4 key购买 nike

哪一个编译器是正确的?

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 版本 4.8.5 (Ubuntu 4.8.5-2ubuntu1~14.04.1)

导致错误:clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based 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 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 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 可以访问私有(private)成员,因此在同一访问中应考虑其默认参数。但是我在字里行间读到 14.7.1(12) 适用于成员模板,而不仅仅是函数模板。另外我可能误解了 14.7.1(12) 的意思。

关于c++ - 私有(private)静态成员可以用作其类的成员函数的默认参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38030334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com