The conditional statement cannot resolve overloaded member function pointers

I have a little problem with pointers to overloaded member functions in C ++. The following code compiles fine:

class Foo {
public:
    float X() const;
    void X(const float x);
    float Y() const;
    void Y(const float y);
};

void (Foo::*func)(const float) = &Foo::X;

But this does not compile (the compiler complains that overloads are ambiguous):

void (Foo::*func)(const float) = (someCondition ? &Foo::X : &Foo::Y);

Presumably, is this somehow related to the compiler, which sorts the return value of the conditional statement separately from the type of the function pointer? I can get around this, but I am interested to know how the specification says that all this should work, since it seems a little unintuitive and if there is a way to get around it without backing down to 5 lines of if-then-else.

I use MSVC ++ if that matters.

Thanks!

+5
3

13.4/1 ( " ", [over.over]):

, - . . - , , .

  • (8.5, 8.5.3),
  • (5.17),
  • (5.2.2),
  • (13.5),
  • , (6.6.3)
  • (5.2.3, 5.2.9, 5.4).

&. , . [: , , (5.1). ]

, , , . , , .

, - . typedef:

typedef void (Foo::* float_func)(const float);
float_func func = (someCondition ? float_func(&Foo::X) : float_func(&Foo::Y));
+7

:

class Foo {
public:
    void X(float x) {}
    void Y(float y)  {}
    float X() const;
};
typedef void (Foo::*Fff)(float);
Fff func = &Foo::X;
Fff func2 = true ? (Fff)&Foo::X : (Fff)&Foo::Y;

int main(){
    return 0;
}

Foo:: X, . , float X(), .

, , ( ).

+1

Try:

    void (Foo::*func1)(const float) = &Foo::X;
    void (Foo::*func2)(const float) = &Foo::Y;

    void (Foo::*func3)(const float) = (someCondition ? func1:func2);

, . , . , .

+1

All Articles