Topic
  • 6 replies
  • Latest Post - ‏2011-03-10T16:32:38Z by Michael_Wong
omkarpl
omkarpl
3 Posts

Pinned topic User-Defined conversion issue

‏2010-07-29T04:35:38Z |
Hi,
We are facing the below issue on XLC AIX compiler, do we have a patch-fix for the same ?

Issue – Exception caused by user-defined conversion.

Activity – Migration of C++ Application from other OS to IBM-AIX.

Description –
1) We have a fully developed C++ Application presently running on production(on non-AIX operating systems).
2) We are in process of porting this on IBM-AIX operating system.
3) However we are facing an issue where the compiler is throwing an error for all the scenario’s with matching constructor of class A and cast-operator of class B.

Simulation of the issue on sample code –
struct C {};
struct A {
A();
A(const C &);
A(const A &);
};
struct B {
operator A() const { A a ; return a;};
operator C() const { C c ; return c;};
};
void f(A x) {};
int main(){
B b;
f((A)b); // The call matches two constructors for A instead of calling operator A()
return 0;
}

Note –
1) This issue is being cited on IBM site along with workarounds at http://publib.boulder.ibm.com/infocenter/zos/v1r9/index.jsp?topic=/com.ibm.zos.r9.cbcmg01/cbcmg16072.htm

Additional Details –

1) The workarounds suggested are not feasible in our case, as it implies a massive modification to the code at various places.
a. Changing f((A)b) to the explicit call f(b.operator A()) – Massive code change.
b. Removing the constructor A(const C &) – We require this constructor as it is referenced independently at various places.
c. Adding a constructor A(B)- Not feasible match, since it means a different implementation, and takes a different path which interferes with other domains(eg-memory manager).
d. Removing either operator A() or operator C()- both are required for casting.
2) Do/Could we have a fix or patch for the above issue?
Updated on 2011-03-10T16:32:38Z at 2011-03-10T16:32:38Z by Michael_Wong
  • flodstrom
    flodstrom
    58 Posts

    Re: User-Defined conversion issue

    ‏2010-08-03T15:20:39Z  
    This is not an answer to your issue, but there are a few things I'm curious about.

    What OS and compiler accepted code like that?

    Also, what is your code supposed to do?
  • omkarpl
    omkarpl
    3 Posts

    Re: User-Defined conversion issue

    ‏2010-08-04T07:30:20Z  
    • flodstrom
    • ‏2010-08-03T15:20:39Z
    This is not an answer to your issue, but there are a few things I'm curious about.

    What OS and compiler accepted code like that?

    Also, what is your code supposed to do?
    Thanks for your reply.

    The above kind of code is working good on Solaris as well as Windows.

    This is a part of massive code, which is written to perform specific functions.

    And the AIX does not allow me to write the code with above kind of structure.

    Not sure why does it find ambiguous ? Why does it try to confuse an operator with constructor, when an explicit typecase is shown.

    f((A)b); can be replaced as - "A temp = (A)b" and it does not work even then.
    Thanks.
  • flodstrom
    flodstrom
    58 Posts

    Re: User-Defined conversion issue

    ‏2010-08-05T10:57:58Z  
    • omkarpl
    • ‏2010-08-04T07:30:20Z
    Thanks for your reply.

    The above kind of code is working good on Solaris as well as Windows.

    This is a part of massive code, which is written to perform specific functions.

    And the AIX does not allow me to write the code with above kind of structure.

    Not sure why does it find ambiguous ? Why does it try to confuse an operator with constructor, when an explicit typecase is shown.

    f((A)b); can be replaced as - "A temp = (A)b" and it does not work even then.
    Thanks.
    I think the source of the problem is the casting you are doing when calling the function.

    I guess it depends on who you are asking, but I personally think that it's rather dangerous to do C style castings like that (especially if the two classes are not even related?). That is why I was a bit surprised to see that it's working at all.

    That said, have you tried to remove the casting (as in "f(b)" only)? I think the xlC compiler might stop complaining, but it may not work as expected either.

    Have you contacted IBM support about this?
  • omkarpl
    omkarpl
    3 Posts

    Re: User-Defined conversion issue

    ‏2010-08-06T05:07:35Z  
    • flodstrom
    • ‏2010-08-05T10:57:58Z
    I think the source of the problem is the casting you are doing when calling the function.

    I guess it depends on who you are asking, but I personally think that it's rather dangerous to do C style castings like that (especially if the two classes are not even related?). That is why I was a bit surprised to see that it's working at all.

    That said, have you tried to remove the casting (as in "f(b)" only)? I think the xlC compiler might stop complaining, but it may not work as expected either.

    Have you contacted IBM support about this?
    Hey ... Yes your suspicion is close.

    "f(b)" is working, but in that case as you see I dont seem to have any control of where the compiler takes me to.

    Though both the classes are not related, both are user-defined and hence logically related and I wish to cast them in a way I know ...

    I am considering the option of contacting IBM Support, however thought of raising it on common forums parallel y.
  • Harvey.Singh
    Harvey.Singh
    1 Post

    Re: User-Defined conversion issue

    ‏2010-12-12T22:20:27Z  
    omkarpl wrote:
    Hey ... Yes your suspicion is close.

    "f(b)" is working, but in that case as you see I dont seem to have any control of where the compiler takes me to.

    Though both the classes are not related, both are user-defined and hence logically related and I wish to cast them in a way I know ...

    I am considering the option of contacting IBM Support, however thought of raising it on common forums parallel y.

    Thanks for your sharing! Now I got it.
  • Michael_Wong
    Michael_Wong
    1 Post

    Re: User-Defined conversion issue

    ‏2011-03-10T16:32:38Z  
    • omkarpl
    • ‏2010-08-06T05:07:35Z
    Hey ... Yes your suspicion is close.

    "f(b)" is working, but in that case as you see I dont seem to have any control of where the compiler takes me to.

    Though both the classes are not related, both are user-defined and hence logically related and I wish to cast them in a way I know ...

    I am considering the option of contacting IBM Support, however thought of raising it on common forums parallel y.
    HI, I believe we are conforming. The issue here is that with the
    explicit cast, we are doing direct-initialization of an A, so we look at
    all constructors of A, and neither one is better. For calling f(b), we
    are looking for an implicit conversion to A, and we don't consider a
    conversion path that involves two user-defined conversions.