转换构造函数 [英] conversion constructor

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

问题描述




以下不起作用。我不明白为什么,因为它可以工作

如果我将(1)标记的行替换为下面的行((2))。使用gcc4.0编译

导致错误:从''两个< int>''转换为非标量

类型''One< int>''请求" ;.有人给我一个解释吗?

模板< typename T>

struct转换

{

typedef T输入;

};


模板< typename T>

class One

{

公开:

一个(){}


模板< typename I>

一个( const typename转换< I> :: Type& rhs){} //(1)

//一个(const I& rhs){} //(2)

};


模板< typename S>

class 2

{

};


int

main()

{

One< int> y = 2< int>();

}

如果我通过显式调用替换main()中的构造函数调用,它

两个版本再次有效。我知道这两个电话的区别(

这么认为),但我无法解释不同的结果。


问候,

Alex



the following does not work. I do not understand why, since it works
if I replace line marked by (1) with the line below ((2)). Compiling
with gcc4.0 results in "error: conversion from ''Two<int>'' to non-scalar
type ''One<int>'' requested". Has somebody an explanation for me?
template <typename T>
struct Convert
{
typedef T Type;
};

template <typename T>
class One
{
public:
One() {}

template <typename I>
One(const typename Convert<I>::Type &rhs) {} // (1)
// One(const I &rhs) {} // (2)
};

template <typename S>
class Two
{
};

int
main()
{
One<int> y = Two<int>();
}
If I replace the constructor call in main() by an explicit call, it
works again with both versions. I know the difference of the two calls(
think so), but I cannot explain the different result.

regards,
Alex

推荐答案

Alexander Stippler写道:
Alexander Stippler wrote:


以下不起作用。我不明白为什么,因为它可以工作
如果我用(1)标记的线替换下面的线((2))。使用gcc4.0进行编译会导致错误:从''两个< int>''转换为非标量
类型''One< int>'''request。有人给我一个解释吗?

模板< typename T>
struct转换
{
typedef T Type;
};

模板< typename T>
一级
{
公开:
一(){}

模板< typename I>
一个(const typename转换< I> :: Type& rhs){} //(1)
//一个(const I& rhs){} //(2)
};

模板< typename S>
第二课
{
};

int
main()
{
一个< int> y = 2< int>();


尝试:


一个< int> y(两个< int>());

}

如果我通过显式调用替换main()中的构造函数调用,它将再次使用两个版本。我知道这两个电话的区别(
这么认为),但我无法解释不同的结果。

问候,
Alex


the following does not work. I do not understand why, since it works
if I replace line marked by (1) with the line below ((2)). Compiling
with gcc4.0 results in "error: conversion from ''Two<int>'' to non-scalar
type ''One<int>'' requested". Has somebody an explanation for me?
template <typename T>
struct Convert
{
typedef T Type;
};

template <typename T>
class One
{
public:
One() {}

template <typename I>
One(const typename Convert<I>::Type &rhs) {} // (1)
// One(const I &rhs) {} // (2)
};

template <typename S>
class Two
{
};

int
main()
{
One<int> y = Two<int>();
Try:

One<int> y( Two<int>() );
}
If I replace the constructor call in main() by an explicit call, it
works again with both versions. I know the difference of the two calls(
think so), but I cannot explain the different result.

regards,
Alex




干杯! --M



Cheers! --M


Alexander Stippler写道:
Alexander Stippler wrote:
以下不起作用。我不明白为什么,因为它可以工作
如果我用(1)标记的线替换下面的线((2))。使用gcc4.0进行编译会导致错误:从''两个< int>''转换为非标量
类型''One< int>'''request。有人给我一个解释吗?

模板< typename T>
struct转换
{
typedef T Type;
};

模板< typename T>
一级
{
公开:
一(){}

模板< typename I>
一个(const typename转换< I> :: Type& rhs){} //(1)
//一个(const I& rhs){} //(2)
};

模板< typename S>
第二课
{
};

int
main()
{
一个< int> y = 2< int>();


所以,让我们试着看看这里发生了什么。如果你使用(2),你的''我'是

任何东西,对吧(c-tor是模板)?因此,您声明''One< T>''以

可以通过const引用从任何类型构造。通过编译器为您定义的复制构造函数
,也可以从另一个''One< T>''构造出来。

上面这一行实际上和


typedef一样< int> oi;


oi y(static_cast< oi>(两个< int>()));


IOW,它构造一个临时对象类型为Two< int>,然后使用用户 -

定义的转换来构造另一个临时的One< int>,然后复制 -

构造''y''来自后者是暂时的。然后所有的临时工都被吹走了。




现在,如果你引入Convert< I>会发生什么?那里。你有相同的

表格


oi y(static_cast< oi>(Two< int>()));


但现在,static_cast怎么能完成它的工作?没有转换来自

两个< int>到一个< int>,只能从Convert< ???> :: Type,给出???以某种方式想出来的是
。对于构造函数,模板参数可以从参数本身的类型中找出_only_

,而不是来自某个其他模板的成员。如果构造函数的参数是''Two< int>'',那么你从编译器那里得到的是弄清楚

''我'是什么。没有

直截了当,毫不含糊的方式来解决这个问题。

}

如果我替换main()中的构造函数调用通过显式调用,


显式调用* what *?某些功能?

它/
再次适用于这两个版本。我知道这两个调用的区别(
这么认为),但我无法解释不同的结果。
the following does not work. I do not understand why, since it works
if I replace line marked by (1) with the line below ((2)). Compiling
with gcc4.0 results in "error: conversion from ''Two<int>'' to non-scalar
type ''One<int>'' requested". Has somebody an explanation for me?
template <typename T>
struct Convert
{
typedef T Type;
};

template <typename T>
class One
{
public:
One() {}

template <typename I>
One(const typename Convert<I>::Type &rhs) {} // (1)
// One(const I &rhs) {} // (2)
};

template <typename S>
class Two
{
};

int
main()
{
One<int> y = Two<int>();
So, let''s try to see what happens here. If you use the (2), your ''I'' is
anything, right (the c-tor is a template)? So, you declare ''One<T>'' to
be constructible from any type through a const reference. It is also
constructible from another ''One<T>'', through a copy constructor defined
by the compiler for you. The line above is, in fact the same as

typedef One<int> oi;

oi y( static_cast<oi>(Two<int>()) );

IOW, it constructs a temporary object of type Two<int>, then uses the user-
defined converstion to construct another temporary of One<int>, then copy-
constructs ''y'' from the latter temporary. Then all temporaries are blown
away.

Now, what happens if you introduce Convert<I> there. You have the same
form

oi y( static_cast<oi>(Two<int>()) );

but now, how can ''static_cast'' do its job? There is no conversion from
Two<int> to One<int>, only from Convert<???>::Type, given that ??? is
somehow figured out. For constructors, the template argument can _only_
be figured out from the type of the argument itself, not from a member of
some other template. What you''re asking from the compiler is to figure out
what ''I'' is if the argument to the constructor is ''Two<int>''. There is no
straight-forward, unambiguous, way to figure that out.
}
If I replace the constructor call in main() by an explicit call,
An explicit call to *what*? To some function?
it
works again with both versions. I know the difference of the two calls(
think so), but I cannot explain the different result.




来自函数的_deducing_模板参数的上下文打电话比建设者更灵活。


V



The contexts for _deducing_ template arguments from a function call are much
more flexible than that for a constructor.

V




mlimber写道:

mlimber wrote:
Alexander Stippler写道:
Alexander Stippler wrote:



[snip]


[snip]

int
main()
{
一个< int> y = 2< int>();
int
main()
{
One<int> y = Two<int>();



尝试:

一个< int> y(两个< int>());



Try:

One<int> y( Two<int>() );

}

如果我通过显式调用替换main()中的构造函数调用,它
再次使用这两个版本。我知道这两个电话的区别(
这么认为),但我无法解释不同的结果。
}
If I replace the constructor call in main() by an explicit call, it
works again with both versions. I know the difference of the two calls(
think so), but I cannot explain the different result.




哦,对。你知道的。好吧,这可能是因为编译器

没有通过实例化另一个模板来进行转换查找。我是

确定这里的一位语言律师可以引用

这段标准作为你的标准。


干杯! --M



Oh, right. You knew that. Well, it''s probably because the compiler
doesn''t do a conversion lookup by instantiating another template. I''m
sure one of the language lawyers around here can cite the paragraph of
the standard for you.

Cheers! --M


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

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