Private conversion function leads to "ambiguous default conversion" error (C ++)

First, a tiny example demonstrating the problem:

struct Bar {
    enum Baz {aa, bb, cc};
    Baz baz_;

    operator Baz() const { return baz_; }
    private:
        template<typename T> operator T() const;
};

int main() {
    Bar bar;
    switch (bar) {
        case Bar::aa:
            break;
        case Bar::bb:
            break;
        case Bar::cc:
            break;
        default:
            break;
    }
    return 0;
}

Compiling this code with g ++ 4.7.0 gives the following error:

foo.cpp: In functionint main()’:
foo.cpp:12:16: error: ambiguous default type conversion fromBarfoo.cpp:12:16: error:   candidate conversions includetemplate<class T> Bar::operator T() const

I understand that since the struct object is "enabled", the compiler will try to find the conversion function for an integer or enumerated type. I explicitly provide a public function to convert to the enum type Bar :: Baz and would like to use it.

, , , . ? , switch((int)bar), , , , . , , ?

, ( 100%) g++ 4.6.

edit: , , .

+5
2

. , §13.3

- , -, . - , -, ( -) -. [: , , . , , . ]

, , .

+3
switch(expression)

, .

-1

All Articles