C ++隐式转换 [英] C++ implicit conversions

查看:222
本文介绍了C ++隐式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于我最近的答复的几条意见, http://stackoverflow.com/questions/862858/what-other-useful-casts-can-be-used-in-c/862896#862896 ,建议我对C ++转换的理解是有缺陷的。为了澄清问题,请考虑以下代码:

  #include< string& 

struct A {
A(const std :: string& s){}
};

void func(const A& a){
}

int main(){
func(one); // error
func(A(two)); // ok
func(std :: string(three)); // ok
}



我的断言是第一个函数调用是一个错误, becauuse没有从const char *转换为A.有一个从字符串到A的转换,但使用这将涉及多个转换。我的理解是,这是不允许的,这似乎是由g ++ 4.4.0& Comeau编译器。 Comeau,我得到以下错误:

 ComeauTest.c,第11行:错误:没有合适的构造函数
从const char [4]转换为A
func(one); //错误

如果你能指出,我错了, ,


最多只有一个用户定义的转换
(构造函数或转换函数)
隐式应用于单个值。


感谢Abhay提供报价。

解决方案

我认为从尖锐的答案是精确的。标题为Conversions的C ++标准(SC22-N-4411.pdf)第12.3.4节清楚地表明只允许一个隐式的用户定义转换。


1类对象的类型转换可以通过
构造函数和转换
函数指定。这些
转换被称为用户定义转换,并且用于
用于隐式类型转换(条款
4),用于
初始化(8.5)和用于显式类型转换(5.4,
5.2.9)。



2用户定义的转换仅适用于
无歧义(10.2,12.3.2)。
转换遵守
访问控制规则(第11条)。访问控制在
模糊度解析(3.4)之后应用。



3 [注:关于转换使用的讨论,请参见13.3调用以及下面的示例
。 -end
note]



4最多一个用户定义的转换(构造函数或转换
函数)隐式应用于
单个
值。



Several comments on a recent answer of mine, http://stackoverflow.com/questions/862858/what-other-useful-casts-can-be-used-in-c/862896#862896, suggest that my understanding of C++ conversions is faulty. Just to clarify the issue, consider the following code:

#include <string>

struct A {
    A( const std::string & s ) {}
};

void func( const A & a ) {
}

int main() {
    func( "one" );                  // error
    func( A("two") );	        // ok
    func( std::string("three") );   // ok
}

My assertion was that the the first function call is an error, becauuse there is no conversion from a const char * to an A. There is a conversion from a string to an A, but using this would involve more than one conversion. My understanding is that this is not allowed, and this seems to be confirmed by g++ 4.4.0 & Comeau compilers. With Comeau, I get the following error:

"ComeauTest.c", line 11: error: no suitable constructor exists 
      to convert from "const char [4]" to "A"
      func( "one" );    		        // error

If you can point out, where I am wrong, either here or in the original answer, preferably with reference to the C++ Standard, please do so.

And the answer from the C++ standard seems to be:

At most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value.

Thanks to Abhay for providing the quote.

解决方案

I think the answer from sharptooth is precise. The C++ Standard (SC22-N-4411.pdf) section 12.3.4 titled 'Conversions' makes it clear that only one implicit user-defined conversion is allowed.

1 Type conversions of class objects can be specified by constructors and by conversion functions. These conversions are called user-defined conversions and are used for implicit type conversions (Clause 4), for initialization (8.5), and for explicit type conversions (5.4, 5.2.9).

2 User-defined conversions are applied only where they are unambiguous (10.2, 12.3.2). Conversions obey the access control rules (Clause 11). Access control is applied after ambiguity resolution (3.4).

3 [ Note: See 13.3 for a discussion of the use of conversions in function calls as well as examples below. —end note ]

4 At most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value.

这篇关于C ++隐式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆