30

Suppose you have this:

struct Foo {
    Foo(unsigned int x) : x(x) {}
    unsigned int x;
};

int main() {
    Foo f = Foo(-1);     // how to get a compiler error here?
    std::cout << f.x << std::endl;
}

Is it possible to prevent the implicit conversion?

The only way I could think of is to explicilty provide a constructor that takes an int and generates some kind of runtime error if the int is negative, but it would be nicer if I could get a compiler error for this.

I am almost sure, that there is a duplicate, but the closest I could find is this question which rather asks why the implicit conversion is allowed.

I am interested in both, C++11 and pre C++11 solutions, preferably one that would work in both.

Community
  • 1
  • 1
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185

3 Answers3

30

Uniform initialization prevents narrowing.

It follows a (not working, as requested) example:

struct Foo {
    explicit Foo(unsigned int x) : x(x) {}
    unsigned int x;
};

int main() {
    Foo f = Foo{-1};
    std::cout << f.x << std::endl;
}

Simply get used to using the uniform initialization (Foo{-1} instead of Foo(-1)) wherever possible.

EDIT

As an alternative, as requested by the OP in the comments, a solution that works also with C++98 is to declare as private the constructors getting an int (long int, and so on).
No need actually to define them.
Please, note that = delete would be also a good solution, as suggested in another answer, but that one too is since C++11.

EDIT 2

I'd like to add one more solution, event though it's valid since C++11.
The idea is based on the suggestion of Voo (see the comments of Brian's response for further details), and uses SFINAE on constructor's arguments.
It follows a minimal, working example:

#include<type_traits>

struct S {
    template<class T, typename = typename std::enable_if<std::is_unsigned<T>::value>::type>
    S(T t) { }
};

int main() {
    S s1{42u};
    // S s2{42}; // this doesn't work
    // S s3{-1}; // this doesn't work
}
skypjack
  • 49,335
  • 19
  • 95
  • 187
27

You can force a compile error by deleting the undesired overload.

Foo(int x) = delete;
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • 7
    `Foo f(42)` would be prohibited (even if `42` is positive) (whereas `Foo f(42u)` works). – Jarod42 Mar 22 '16 at 18:14
  • 1
    @Jarod42, true. But unvoidable :) – SergeyA Mar 22 '16 at 18:20
  • @Sergey For compile time constants we could avoid it with some SFINAE overloading and helper methods I guess, but I don't think one could combine the two methods as to disallow ints in general but allow int constants. – Voo Mar 23 '16 at 09:29
8

If you want to be warned on every occurrence of such code, and you're using GCC, use the -Wsign-conversion option.

foo.cc: In function ‘int main()’:
foo.cc:8:19: warning: negative integer implicitly converted to unsigned type [-Wsign-conversion]
     Foo f = Foo(-1);     // how to get a compiler error here?
                   ^

If you want an error, use -Werror=sign-conversion.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82