Topic: key word explicit in function arguments
Author: "Sid" <wizard@gte.net>
Date: 1997/11/13 Raw View
>class String
>{
> public:
> String (const char* s);
>};
>
>void foo (explicit const String& str)
>{
> // do whatever
>}
>int main ()
>{
> char* s = "hello";
> foo (s); // I would like this to produce a compile time error
>}
I don't think the above example is legal. It is, however, not
necessary. When you invoke the function foo, the compiler must create
a temporary String object from the char pointer parameter.
If String::String(const char*) were declared explicit, I think this
would prohibt the compiler from creating the temporary and thus the
function call will produce a compile error.
Someone please correct me if I am wrong.
DV
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Mohammad Jafar <jafar@esp.bellcore.com>
Date: 1997/11/05 Raw View
I think being able to use the keyword explicit in
the declaration of a constructor is a great idea.
Now my question is can it be also used in parameter
specification.
Example:
class String
{
public:
.
.
String (const char* s);
.
.
};
void foo (explicit const String& str)
{
// do whatever
}
int main ()
{
.
.
char* s = "hello";
foo (s); // I would like this to produce a compile time error
.
.
}
Is this feature available in the latest C++ language specification ?
Does it break something ? Is it hard to implement in the compiler ?
If there are no problems with this feature. I think it would offer
greater flexibility in controlling implicit conversions.
--
Mohammad Jafar
jafar@esp.bellcore.com
(732) 699-8352
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/11/05 Raw View
Mohammad Jafar wrote:
> I think being able to use the keyword explicit in
> the declaration of a constructor is a great idea.
Me too
> Now my question is can it be also used in parameter
> specification.
>
> Example:
[const char* convertible to String]
> void foo (explicit const String& str)
> {
> // do whatever
> }
>
> int main ()
> {
> .
> .
> char* s = "hello";
> foo (s); // I would like this to produce a compile time error
> .
> .
> }
>
> Is this feature available in the latest C++ language specification ?
> Does it break something ? Is it hard to implement in the compiler ?
No, no, no(trivial)
> If there are no problems with this feature. I think it would offer
> greater flexibility in controlling implicit conversions.
I don't think it's usefull enough to include it.
Either viewing a const char* as a String is ok or not. If not,
then you shouldn't define a non-explicit ctor.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]