Re: Flag to Stop Implicit Conversion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Roopesh,

Although I, too, desire the same kind of warning you mention, it just isn't
C or C++.

Moreso, I wish I could disable all implicit conversions.  But, doing so,
would be a language that isn't C or C++.  It would be an "almost C" or
"almost C++" language, which would break a lot of code.

What you need is a different language, such as Ada or D Programming
Language.  Both of which are available for GCC.

Or in C, you need to wrap your int in a struct.  Like this:

----------------------------------------
#include <stdio.h>

typedef struct myint_s
{
  int m;
} myint;

void Foo(myint in)
{
  printf("%d\n", in.m);
}

enum Numero { uno, dos, tres };

int main()
{
  Foo(uno); // Bzzt.
  Foo(1); // Bzzt.
  myint i = { 1 };
  Foo(i); // Good.
}
----------------------------------------

Or in C++ (q.v. Stroupstrup C++PL 11.7.1)...

----------------------------------------
#include <iostream>

class myint
{
  int m;
public:
  explicit myint(int i) : m(i) { }
  operator int () const { return m; }
};

void Foo(myint in)
{
  std::cout << in << std::endl;
}

enum MetasyntacticSugar { foo, bar, quux };

int main()
{
  Foo(quux); // Bzzt.
  Foo(3); // Bzzt.
  Foo(myint(3)); // Good.
  myint i(3);
  Foo(i); // Good.
} 
----------------------------------------

CAVEAT: I just wrote these off the cuff, I haven't tried to compile them.

HTH,
--Eljay


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux