88

When it comes to constructors, adding the keyword explicit prevents an enthusiastic compiler from creating an object when it was not the programmer’s first intention. Is such mechanism available for casting operators too?

struct Foo
{
    operator std::string() const;
};

Here, for instance, I would like to be able to cast Foo into a std::string, but I don’t want such cast to happen implicitly.

qdii
  • 12,505
  • 10
  • 59
  • 116

1 Answers1

106

Yes and No.

It depends on which version of C++, you're using.

  • C++98 and C++03 do not support explicit type conversion operators
  • But C++11 does.

Example,

struct A
{
    //implicit conversion to int
    operator int() { return 100; }

    //explicit conversion to std::string
    explicit operator std::string() { return "explicit"; } 
};

int main() 
{
   A a;
   int i = a;  //ok - implicit conversion 
   std::string s = a; //error - requires explicit conversion 
}

Compile it with g++ -std=c++0x, you will get this error:

prog.cpp:13:20: error: conversion from 'A' to non-scalar type 'std::string' requested

Online demo : http://ideone.com/DJut1

But as soon as you write:

std::string s = static_cast<std::string>(a); //ok - explicit conversion 

The error goes away : http://ideone.com/LhuFd

BTW, in C++11, the explicit conversion operator is referred to as "contextual conversion operator" if it converts to boolean. Also, if you want to know more about implicit and explicit conversions, read this topic:

starball
  • 20,030
  • 7
  • 43
  • 238
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 9
    Even in C++03, it's easy to avoid the implicit conversion. Just call the function `toString`, rather than `operator std::string`. Of course, this may cause problems with some templates. I've always used `toString`, and it's never caused me any problems, but I imagine that this could depend on your coding style. – James Kanze Nov 23 '11 at 09:15
  • @MatthieuM. Just like `operator std::string()`:-). – James Kanze Nov 23 '11 at 09:51
  • 2
    I use `to_string` instead. It helps that it's what C++11 calls it, so it helps write forwards-compatible code _and_ it helps with templates. – Luis Machuca Aug 04 '12 at 06:43
  • 1
    `std::string s(a)` or `std::string s{a}` should also work as `static_cast(a)`. – alfC Dec 24 '13 at 04:05
  • In C++ 11, things like "if(std::cin)" is legal. Since the 'istream' class has a member "explicit operator bool() const;" why doesn't the compiler enforce the code to be "if(static_cast(std::cin))" ? – Bin Apr 05 '15 at 08:12
  • 2
    @Bin: Because the `explicit operator bool()` is invoked *contextually* by the compiler when you write `if(std::cin)`. Note the conversion which occurs here is (informally) called *contextual* conversion, **not** *implicit* conversion. – Nawaz Apr 05 '15 at 18:55