using declaration to introduce a name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mzdude

    using declaration to introduce a name

    I have the following code

    struct foo
    {
    operator std::string () const { return str;}
    std::string f() const {return str;}

    std::string str;
    };

    struct bar : private foo
    {
    using foo::operator std::string;
    using foo::f;
    };

    void test()
    {

    bar b;
    std::string a = b; // Ok
    std::string a2 = b.f(); // Ok

    const bar b2;
    std::string a3 = b2; // Compile error
    std::string a4 = b2.f(); // Ok
    }

    My question is why does the compiler have a problem with the const
    for the operator but not the function.


    error XXXX: 'initializing' : cannot convert from 'const bar' to
    'std::basic_str ing<_Elem,_Trai ts,_Ax>'
    with
    [
    _Elem=char,
    _Traits=std::ch ar_traits<char> ,
    _Ax=std::alloca tor<char>
    ]
    No constructor could take the source type, or constructor
    overload resolution was ambiguous
  • Victor Bazarov

    #2
    Re: using declaration to introduce a name

    mzdude wrote:
    I have the following code
    >
    struct foo
    {
    operator std::string () const { return str;}
    std::string f() const {return str;}
    >
    std::string str;
    };
    >
    struct bar : private foo
    {
    using foo::operator std::string;
    using foo::f;
    };
    >
    void test()
    {
    >
    bar b;
    std::string a = b; // Ok
    std::string a2 = b.f(); // Ok
    >
    const bar b2;
    Comeau justifiably complains about that declaration/definition, it has
    no initialiser. Change it to

    const bar b2(b);

    and the code compiles fine.
    std::string a3 = b2; // Compile error
    std::string a4 = b2.f(); // Ok
    }
    >
    My question is why does the compiler have a problem with the const
    for the operator but not the function.
    Because of the bug in the compiler, maybe?

    Post the same question to 'microsoft.publ ic.vc.language' (you *are*
    using MSVC, aren't you?)
    >
    >
    error XXXX: 'initializing' : cannot convert from 'const bar' to
    'std::basic_str ing<_Elem,_Trai ts,_Ax>'
    with
    [
    _Elem=char,
    _Traits=std::ch ar_traits<char> ,
    _Ax=std::alloca tor<char>
    ]
    No constructor could take the source type, or constructor
    overload resolution was ambiguous

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • mzdude

      #3
      Re: using declaration to introduce a name

      On Oct 8, 3:55 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
      >
      Because of the bug in the compiler, maybe?
      >
      Post the same question to 'microsoft.publ ic.vc.language' (you *are*
      using MSVC, aren't you?)
      >
      Yes. I'm using VC8. That's what I wanted to know. Was it a compiler
      bug or a definition problem.

      Comment

      Working...