-4

C++11 adds this.

int x;
unsigned int y{ x }; // ERROR

Is it possible to enable something like this.

int x;
void f(unsigned int y);
f(x); //ERROR

Compiler: VC++ 2013

NFRCR
  • 5,304
  • 6
  • 32
  • 37

2 Answers2

0

Try this (live example):

template <
    typename T,
    typename = typename std::enable_if <std::is_same <T, unsigned int>{}>::type
>
void f(T x) { }
iavr
  • 7,547
  • 1
  • 18
  • 53
0

No, there's no compiler switch or other general setting to do that. Implicit conversions are part of the language, and cannot be disabled in the general case for built-in types. The closest you can get is a user-defined wrapper class with only explicit constructors, or applying some template meta-hackery to the function you're trying to call (as in iavr's answer).

The premise of your question appears to conflate implicit conversions with the special case of narrowing conversions. The following:

int x = 0;
unsigned int y{ x };

is not an error. You may get a warning about the potential narrowing conversion, and this warning may be turned into an error (with GCC's -Werror, for example), but this is not new in C++11 and it does not inherently prohibit the conversion.

The program is only ill-formed if you actually cause a narrowing conversion. Again, this is specific to the value involved and is not a general rule about implicit conversions.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055