以下のコードをコンパイルするとエラーになる。
class X
{
    int value ;
    friend class Y ;
} ;
class Y
{
public :
    template < typename T >
    static auto f(T t) -> decltype( t.value )
    {
        return t.value ;
    }
} ;
 
int main()
{
    X x ;
    Y::f(x) ;
}
gccは、おそらくバグのため、このコードをコンパイルできない。興味深いのは、エラーメッセージだ。
In function 'int main()': error: 'int X::value' is private error: within this context
そもそもmain関数内というのが間違っている。。X::valueにアクセスしているのは、Y::get関数である。
試しに、クラスXからmain関数をfriendにしてみると。
class X
{
    int value ;
    friend class Y ;
    friend int main() ;
} ;
なぜかコンパイルが通ってしまう。明らかにおかしい。
なかなか笑えるバグだ。発見者はバグレポートを投げたほうがいいと思う。
No comments:
Post a Comment
You can use some HTML elements, such as <b>, <i>, <a>, also, some characters need to be entity referenced such as <, > and & Your comment may need to be confirmed by blog author. Your comment will be published under GFDL 1.3 or later license with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.