Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Doesn't "explicit" mean explicit?

71 views
Skip to first unread message

DSF

unread,
Jan 15, 2015, 1:58:26 PM1/15/15
to
Hello!

I've always assumed that the keyword explicit produced a compiler
error if the argument was not the *exact* type.

explicit FBaseString(size_t bufferlen);
explicit FBaseString(CH ch);

CH can only be char or wchar_t.
The above produces the error:
Error: readdirs.cpp(287,32):Ambiguity between
'FBaseString<wchar_t>::FBaseString(unsigned int)' and
'FBaseString<wchar_t>::FBaseString(wchar_t)'

I assume this means explicit allows promotions before type checking.

Is this the case?

Thanks,
DSF
"'Later' is the beginning of what's not to be."
D.S. Fiscus

Marcel Mueller

unread,
Jan 15, 2015, 3:48:29 PM1/15/15
to
On 15.01.15 19.58, DSF wrote:
> I've always assumed that the keyword explicit produced a compiler
> error if the argument was not the *exact* type.

No.

explicit prevents from implicit constructor invocation without using the
constructors name (= class name), e.g. for copy construction.


Marcel

red floyd

unread,
Jan 15, 2015, 4:54:01 PM1/15/15
to
On 1/15/2015 10:58 AM, DSF wrote:
> Hello!
>
> I've always assumed that the keyword explicit produced a compiler
> error if the argument was not the *exact* type.
>
> explicit FBaseString(size_t bufferlen);
> explicit FBaseString(CH ch);
>
> CH can only be char or wchar_t.
> The above produces the error:
> Error: readdirs.cpp(287,32):Ambiguity between
> 'FBaseString<wchar_t>::FBaseString(unsigned int)' and
> 'FBaseString<wchar_t>::FBaseString(wchar_t)'
>
> I assume this means explicit allows promotions before type checking.
>
> Is this the case?
>

To provide an example to Marcel's comment, With explicit (excluding
your type clash error), the following happens:

FBaseString x(77); // OK
FBaseString y = FBaseString(77); // also OK
FBaseString z = 77; // ERROR -- constructor is explicit


red floyd

unread,
Jan 15, 2015, 4:56:15 PM1/15/15
to
On 1/15/2015 10:58 AM, DSF wrote:

>
> explicit FBaseString(size_t bufferlen);
> explicit FBaseString(CH ch);
>
> CH can only be char or wchar_t.
> The above produces the error:
> Error: readdirs.cpp(287,32):Ambiguity between
> 'FBaseString<wchar_t>::FBaseString(unsigned int)' and
> 'FBaseString<wchar_t>::FBaseString(wchar_t)'
>


Also, in this case, explicit is a red herring. The issue is that your
compiler is apparently not creating a distinct type for wchar_t, but
using a typedef of some unsigned type.



Victor Bazarov

unread,
Jan 15, 2015, 5:50:25 PM1/15/15
to
Obviously, the same "unsigned int" for which it has a typedef for
'size_t'. I mean, on the close examination of the error message's
penultimate line...

V
--
I do not respond to top-posted replies, please don't ask

DSF

unread,
Jan 18, 2015, 1:36:58 PM1/18/15
to
Thanks for the explanation,

DSF

unread,
Jan 18, 2015, 1:38:32 PM1/18/15
to
On Thu, 15 Jan 2015 13:53:51 -0800, red floyd <no....@its.invalid>
wrote:
Thanks for expanding on Marcel's explanation,

DSF

unread,
Jan 18, 2015, 1:39:14 PM1/18/15
to
"Head of nail, meet hammer." was what I wanted to type because I
found a typedef for wchar_t. But it turns out that it's only used if
__cplusplus is not defined, making wchar_t available for C code. (I've
done much the same thing for bool, true, and false.) wchar_t *is* a
native type.
0 new messages