0

could someone please explain the syntax of the following snippet from a c++ class template?

public:
explicit Foo(size_t ratio = 500)
    :list(Bar<I>())
    ,vec(Bar<iterator>())
    ,n(0), r(ratio){}

I have decent experience with programming and understand most concepts but the syntax behind this is just so foreign to me, i can't understand it. I is the generic type. Specifically, I'm not sure what the explicit is doing here, in addition to what is going on when one would execute some code such as Foo<int> myfoo;.

thanks!

Mikhail
  • 20,685
  • 7
  • 70
  • 146
Gagan Singh
  • 988
  • 12
  • 22
  • 1
    This answers the what is explicit nicely: http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean – Kyle C Mar 28 '13 at 21:15
  • You need [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?rq=1). Please select more than one. – Bo Persson Mar 28 '13 at 21:18

3 Answers3

4

The explicit keyword makes sure that your constructor is not picked as part as a user-defined conversion sequence to perform implicit conversions when copy-initialized with a value of type size_t.

Without the explicit keyword, for instance, all of the following would be valid:

Foo<int> bar(Foo<int> f) 
{ 
    return 0; // OK
}

Foo<int> f = 1729; // OK
bar(42); // OK

Declaring your constructor as explicit prevents the initializations above from compiling.


What follows the constructor's signature is an initialization list, and it is used to construct the base class subobjects and data member subobjects of your class before the body of your constructor is entered.

When you do not use an initialization list, the data members of your class are default-constructed. This, however, may not be an option if your class has const data members, or data members of reference type, or data members of a class type which do not have a default constructor.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
1

This is a constructor for the class Foo, but I'm sure you already knew that.

explicit says that this constructor must receive an argument of size_t. That is, one cannot pass in anything that would be implicitly casted to a type size_t (ex: int).

The rest of the things are part of the constructor's initializer list, which means the constructor initializes the data member list with Bar<I>(), etc.

Tushar
  • 8,019
  • 31
  • 38
0

This is the definition of a constructor for Foo. The constructor is explicit, which means that it won't be involved in implicit conversions. As an example consider:

void bar(Foo);
bar(10); // Error - would require an implicit conversion

Marking a constructor as explicit prevents such conversions.

Everything after the : is a member initialization list. It lists members of Foo by their names and the parentheses contain the expressions with which to initialize them. For example, list will be initialized with the temporary object created by Bar<I>(), n is initialized with 0, etc.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324