What is Explicit in C++?

Asked

Viewed 574 times

9

I came across the term explicit being used in a C code++.

What is the use of this keyword?

  • 1

    I won’t stick to the answer, but look here: http://renangreinert.blogspot.com.br/2011/09/palavra-magica-e-explicit.html

1 answer

8


Operator of cast

It is used along with an operator’s declaration of cast.

I imagine you know that C++ allows you to define operators for a type, so the syntax of certain operations is closer than people are used to traditional mathematics or the one already used in programming.

One such operator is the cast, which is the conversion from one type to another. The operation may be just a reinterpretation of the data or have to change its content. It can be a simple or complex operation (transforming a text into a number is complex).

There are two ways to use this operator, one is explicitly and one is implicit. The way to do each can vary. If implicit is the conversion that the compiler itself identifies and calls the operator on its own. The explicit way is when the programmer put there in the code to ensure that the conversion will be done.

Since you have two operators, you have to identify when is one and when is the other. The explicit is determined with the keyword explicit. The implicit is determined by a method only with the type name without a specific keyword.

operator int() const { return 0; } //obviamente é só exemplo, não está convertendo nada

This would be the operator of cast for a guy int. I’d wear it like this:

int x = (int)variavel_do_tipo_que_tem_o_operador; //x valeria 0 nesse exemplo

You obviously need to have an operator for each type that you want to provide a conversion.

Only available in C++11 forward.

More about the operator of cast.

Builder

The keyword is used in another declaration situation of constructor method which is required to direct startup.

Direct initialization is the construction of an object exclusively through the syntax of methods that everyone knows.

The indirect is a way in which the initialization can occur through the assignment of value to the object. This is not always desirable. You can hide the fact that it’s a builder.

Then an explicit constructor disables the indirect construction

Foo f(2); //forma explícita/direta de chamada do construtor
Foo f2 = 2; //forma implícita/indireta de chamada do construtor

Using

explicit Foo(int) { }

Only the first call would be possible.

Full example:

struct A {
    A(int) { }      // construtor de conversão
    A(int, int) { } // construtor de conversão (C++11)
    operator int() const { return 0; } // operador de cast implícito
};
 
struct B {
    explicit B(int) { }
    explicit B(int, int) { }
    explicit operator int() const { return 0; }
};
 
int main() {
    A a1 = 1;      // OK: copy-initialization chama A::A(int)
    A a2(2);       // OK: direct-initialization chama A::A(int)
    A a3 {4, 5};   // OK: direct-list-initialization chama A::A(int, int)
    A a4 = {4, 5}; // OK: copy-list-initialization chama A::A(int, int)
    int na1 = a1;  // OK: copy-initialization chama A::operator int()
    int na2 = static_cast<int>(a1); // OK: static_cast faz a inicialização
    A a5 = (A)1;   // OK: explicit cast faz o static_cast
 
//  B b1 = 1;      // error: copy-initialization  B::B(int) não permitida
    B b2(2);       // OK: direct-initialization chama B::B(int)
    B b3 {4, 5};   // OK: direct-list-initialization chama B::B(int, int)
//  B b4 = {4, 5}; // error: copy-list-initialization B::B(int,int) não permitida
//  int nb1 = b2;  // error: copy-initialization B::operator int() não permitida
    int nb2 = static_cast<int>(b2); // OK: static_cast faz a inicialização
    B b5 = (B)1;   // OK: explicit cast faz o static_cast
}

I put in the Github for future reference.

  • 1

    Hi bigown. The answer is good, but I think we could improve (the part before the builder, particularly) with some examples of use. For more experienced people I think is clear, but I do not know if it is clear enough for AP (by the way, he could already give some feedback in this sense, né Sérgio?). :)

  • 1

    I didn’t put it in the documentation. I’ll figure something out later.

  • I’ve actually been trying to record the change for some time and it won’t. The site is problematic in some situations.

  • Yes, I thought the answer was very good! Really an example would help a lot, but for me I could already understand. Now the process is the same, if many people visit the question we improve the text, if no one visit we focus more on the other questions! Good observation Luiz :)

  • "It is only available in C++11 forwards." Looking at the surface, it seems that this statement is wrong. The keyword Explicit already existed in C++03.

  • @Joséx. has any reference? The documentation I Linkei informs only C++11. It existed in another context (the second case I pointed out).

  • Okay, there’s actually one case that just popped up on C++11.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.