Dree icon indicating copy to clipboard operation
Dree copied to clipboard

style: declare single argument constructors as `explicit`

Open vil02 opened this issue 1 year ago • 2 comments

This PR declares all of the single argument constructors as explicit. This makes the implicit conversion impossible. Cf.

vil02 avatar Oct 03 '24 17:10 vil02

Why cant this be done for multi param constructors?

ujjwall-R avatar Oct 04 '24 11:10 ujjwall-R

Why cant this be done for multi param constructors?

What is implicit conversion? Please have a look at this example:

struct S
{
    int val_a;
    int val_b;
    S(int in_val) : val_a(in_val), val_b(10*in_val) {}
};

int main()
{
    int some_int = 17;
    S some_s = some_int; // implicit conversion from int to S

    return 0;
}

Here an integer some_int is implicitly converted to an object of class S. We could make the conversion explicit by:

S some_other_s = S(some_int); // we explicitly call the constructor of S

Answering your question: it is simply not possible in C++ to have implicit conversion of several variables.

Please have a look at:

  • https://en.cppreference.com/w/cpp/language/explicit
  • https://en.cppreference.com/w/cpp/language/converting_constructor

vil02 avatar Oct 04 '24 14:10 vil02

Please change the PR merge to dev branch. We will merge in main once we move for release after thorough testing.

ujjwall-R avatar Oct 06 '24 15:10 ujjwall-R