The question itself may be confusing, so I'll describe it in detail here. Suppose we have a type S that represents a 1-dimensional vector (the linear algebra one, not the std one). Since it's kind of an array, so it's good if it behaves like std::array<int, 1>. Obviously, we don't want to allow construction with just a scalar, but implicit list initialization from a single element should be allowed. Shown in the following code snippet are the few initialization/conversion scenarios, I would prefer #2 and #4 to work while rejecting #1 and #3.
struct S
{
explicit(?) S(int value);
};
S f1() { return 0; } // #1
S f2() { return { 0 }; } // #2
void g()
{
S s1 = 0; // #3
S s2{ 0 }; // #4
}
Making the constructor explicit would disallow #1 and #3, which is nice, but as a side effect, it also disallows #2. #2 should be allowed in this scenario, just like how std::array<int, 1> works.
Since the type would have other constructors defined, and I would like the data to be private, so AFAIK it's not possible to use the same technique as std::array (aggregate init + brace elision). Are there any means to achieve this goal? I don't mind if some bizarre tricks must be used.
Thanks!
Edit:
- I have tried
explicit(false) S(std::array<int, 1>);and it works, but it needs an additional pair of braces for cases #2 and #4. It would be great if the same goal can be achieved without the extra braces.