构造函数歧义 [英] constructor ambiguity

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

问题描述




我已经发布并讨论了以下问题一次,但

尽管提供了非常有用的提示我还没有进一步解决我的问题

(我至少学会了,首先要仔细考虑为什么有些东西没有工作而不是立即寻找工作)。我有以下代码的

导致含糊不清:

------------------------ --------------------------------

模板< typename Impl>

class Vector {};


模板< typename参考>

类VectorView:public Vector< VectorView< Ref> >

{

public:

operator const Ref&()const {return _ref; }

私人:

参考_ref;

};


模板< typename T>

class DenseVector:public Vector< DenseVector< T> >

{

public:

DenseVector(){}


template< typename Impl>

DenseVector(const Vector< Impl>& rhs){}

VectorView< DenseVector< T> >

operator()(int from,int to){return VectorView< DenseVector< T>

Hi

I have already posted and discussed the following problems once, but
despite really helpful hints I did not get any further with my problem
(I at least learned, first to exactly consider why something does not
work instead of immediately searching for work arounds) . I have the
following code resulting in an ambiguity:
--------------------------------------------------------
template <typename Impl>
class Vector {};

template <typename Ref>
class VectorView : public Vector<VectorView<Ref> >
{
public:
operator const Ref &() const { return _ref; }
private:
Ref _ref;
};

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

template <typename Impl>
DenseVector(const Vector<Impl> &rhs) {}

VectorView<DenseVector<T> >
operator()(int from, int to) { return VectorView<DenseVector<T>

(); }



};


int amin(const DenseVector< double>& x){return 1; }

int amin(const DenseVector< float>& x){return -1; }


int main()

{

DenseVector< double> x;

int i = amin(x(1,3));

}

---------- ----------------------------------------------

有些事情对我来说并不是很清楚:


1)导致歧义的原因。

类VectorView中的转换运算符返回DenseVector< double>。为什么进一步的

转换被认为是因为我有一个完全匹配的函数amin?


2)它是DenseVector的模板化构造函数导致了麻烦。

我无法明确说明,在示例中可以使用什么。有没有

另一种拥有这个构造函数的方法,但是如果Impl是DenseVector< T2>?
那就防止它被认为是候选人
最好的问候,

Alex


};

int amin(const DenseVector<double> &x) { return 1; }
int amin(const DenseVector<float> &x) { return -1; }

int main()
{
DenseVector<double> x;
int i = amin(x(1,3));
}
--------------------------------------------------------
Some things are not really clear to me:

1) what causes the ambiguity. The conversion operator in
class VectorView returns a DenseVector<double>. Why is any further
conversion considered as I have an exact matching function amin?

2) It''s the templated constructor of DenseVector causing the trouble.
I cannot make it explicit, what would work in the example. Is there
another way to have this constructor, but prevent it from being
considered a candidate, if Impl is DenseVector<T2>?

best regards,
Alex

推荐答案

* Alexander Stippler:
* Alexander Stippler:
<我曾经发布并讨论了以下问题,但是尽管有很多有用的提示,但我没有进一步解决我的问题
(我至少学会了,首先要仔细考虑为什么没有
工作而不是立即寻找工作)。我有以下代码导致含糊不清:
-------------------------------- ------------------------
模板< typename Impl>
类Vector {};

template< typename Ref>
class VectorView:public Vector< VectorView< Ref> >
{
公开:
运算符const Ref&()const {return _ref; }
私人:
参考_ref;
};

模板< typename T>
类DenseVector:public Vector< DenseVector< T> >
公开:
DenseVector(){}

模板< typename Impl>
DenseVector(const Vector< Impl>& rhs ){}

VectorView< DenseVector< T> >
operator()(int from,int to){return VectorView< DenseVector< T>

I have already posted and discussed the following problems once, but
despite really helpful hints I did not get any further with my problem
(I at least learned, first to exactly consider why something does not
work instead of immediately searching for work arounds) . I have the
following code resulting in an ambiguity:
--------------------------------------------------------
template <typename Impl>
class Vector {};

template <typename Ref>
class VectorView : public Vector<VectorView<Ref> >
{
public:
operator const Ref &() const { return _ref; }
private:
Ref _ref;
};

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

template <typename Impl>
DenseVector(const Vector<Impl> &rhs) {}

VectorView<DenseVector<T> >
operator()(int from, int to) { return VectorView<DenseVector<T>
(); }};

int amin(const DenseVector< double>& x){return 1; }
int amin(const DenseVector< float>& x){return -1; }

int main()
{
DenseVector< double> x;
int i = amin(x(1,3));
}
---------------------- ----------------------------------
有些事情对我来说并不是很清楚:
1)导致歧义的原因。
类VectorView中的转换运算符返回DenseVector< double>。为什么进一步的转换被认为是因为我有一个完全匹配的函数amin?
(); } };

int amin(const DenseVector<double> &x) { return 1; }
int amin(const DenseVector<float> &x) { return -1; }

int main()
{
DenseVector<double> x;
int i = amin(x(1,3));
}
--------------------------------------------------------
Some things are not really clear to me:

1) what causes the ambiguity. The conversion operator in
class VectorView returns a DenseVector<double>. Why is any further
conversion considered as I have an exact matching function amin?




因为编译器必须选择一个转换(你还没有指定

one),并且有三个同样优秀的候选人:


VectorView :: operator const Ref()生成一个DenseVector< double>,


DenseVector< float> :: DenseVector(x)产生一个DenseVector< float>,并且

DenseVector< double> :: DenseVector(x)产生一个DenseVector< ; double> ;.


2)它是DenseVector的模板化构造函数导致了麻烦。
我无法明确说明,在示例中会起作用。


为什么?它运行得很好。


是否有其他方法可以使用此构造函数,但如果Impl是DenseVector< T2>,则阻止它被视为候选者。 ?



Because the compiler must choose a conversion (you haven''t specified
one), and there are three equally good candidates:

VectorView::operator const Ref() produces a DenseVector<double>,

DenseVector<float>::DenseVector(x) produces a DenseVector<float>, and

DenseVector<double>::DenseVector(x) produces a DenseVector<double>.

2) It''s the templated constructor of DenseVector causing the trouble.
I cannot make it explicit, what would work in the example.
Why? It works just fine.

Is there
another way to have this constructor, but prevent it from being
considered a candidate, if Impl is DenseVector<T2>?




我不这么认为。你不能将它作为隐式转换,

和非隐式转换。如果你绝对必须拥有它那么

你必须选择。但是你还没有说出它想要解决的问题是什么?

。也许真正的问题是承认一些可接受的解决方案。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和e-最令人烦恼的是什么?邮件?



I don''t think so. You cannot have it as both an implicit conversion,
and as a non-implicit conversion. If you absolutely must have it then
you''ll have to choose. But you haven''t said what problem it''s intended
to solve. Perhaps the real problem admits some acceptable solution.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Alexander Stippler写道:
Alexander Stippler wrote:


我已经发布并讨论了以下问题,但是
尽管提供了非常有用的提示,但我没有进一步了解我的问题
(我至少学会了,首先要确切地考虑为什么某些事情没有工作而不是立即寻找工作)。我有以下代码导致含糊不清:
-------------------------------- ------------------------
template< typename Impl>
DenseVector(const Vector< Impl>& rhs){ 1)导致歧义的原因。
类VectorView中的转换运算符返回DenseVector< double>。为什么进一步的转换被认为是因为我有一个完全匹配的函数amin?

2)它是DenseVector的模板化构造函数导致了麻烦。
我无法做到显式,在示例中将起作用。是否有另一种方法可以使用这个构造函数,但是如果Impl是DenseVector< T2>,则阻止它被视为候选者?
Hi

I have already posted and discussed the following problems once, but
despite really helpful hints I did not get any further with my problem
(I at least learned, first to exactly consider why something does not
work instead of immediately searching for work arounds) . I have the
following code resulting in an ambiguity:
-------------------------------------------------------- template <typename Impl>
DenseVector(const Vector<Impl> &rhs) {}

1) what causes the ambiguity. The conversion operator in
class VectorView returns a DenseVector<double>. Why is any further
conversion considered as I have an exact matching function amin?

2) It''s the templated constructor of DenseVector causing the trouble.
I cannot make it explicit, what would work in the example. Is there
another way to have this constructor, but prevent it from being
considered a candidate, if Impl is DenseVector<T2>?




是的,问题是你有一个构造函数允许任何带有任何模板参数的任何

DenseVector隐式转换到任何其他带有任何其他模板的DenseVector

参数。避免非平凡的隐式构造函数的另一个原因。

为什么你不明白?


-

Valentin Samko - http://www.valentinsamko.com


在< 43 *********************** @ authen.white.readfreene ws.net> Valentin

Samko写道:
In <43***********************@authen.white.readfreene ws.net> Valentin
Samko wrote:
Alexander Stippler写道:
Alexander Stippler wrote:


我已经发布并讨论了以下问题曾经,但
尽管有非常有用的提示,我没有得到任何进一步的问题(我至少学会了,首先要仔细考虑为什么
不会立即工作而不是立即工作寻找工作)。我有以下代码导致含糊不清:
-------------------------------- ------------------------
Hi

I have already posted and discussed the following problems once, but
despite really helpful hints I did not get any further with my
problem (I at least learned, first to exactly consider why something
does not
work instead of immediately searching for work arounds) . I have the
following code resulting in an ambiguity:
--------------------------------------------------------


template< typename Impl>
DenseVector(const Vector< Impl>& rhs){}

1)导致歧义的原因。
类VectorView中的转换运算符返回DenseVector< double>。为什么进一步的转换被认为是因为我有一个完全匹配的函数amin?

2)它是DenseVector的模板化构造函数导致了麻烦。
我无法做到显式,在示例中将起作用。是否有另一种方法可以使用这个构造函数,但是如果Impl是DenseVector< T2>,则阻止它被视为候选者?
template <typename Impl>
DenseVector(const Vector<Impl> &rhs) {}

1) what causes the ambiguity. The conversion operator in
class VectorView returns a DenseVector<double>. Why is any further
conversion considered as I have an exact matching function amin?

2) It''s the templated constructor of DenseVector causing the trouble.
I cannot make it explicit, what would work in the example. Is there
another way to have this constructor, but prevent it from being
considered a candidate, if Impl is DenseVector<T2>?



是的,问题是你有一个构造函数,它允许从任何带有任何模板参数的DenseVector隐式转换到任何其他带有任何其他模板参数的DenseVector。另一个原因是为了避免使用非平凡的隐式构造函数。

为什么不能说明它?

-

Valentin Samko - http://www.valentinsamko.com



原则上你是对的。但就我的目的而言,构造函数的''赋值符号''

是自然的方式。我真正想要的是显式和隐式的混合:

一个显式构造函数,但允许对象A =某事; as

隐式初始化。但是我找到了解决问题的方法:

给构造函数第二个参数默认初始化

并限制SFINAE构造函数的可用性为

第二个参数。因此,我可以明确地控制构造函数可用的实例,而不是。

感谢您的帮助。


问候,

Alex



In principle you''re right. But for my purposes the ''assign notation''
for constructor is the natural way. What I would really like is a
mixture of explicit and implicit:
an explicit constructor, but allowing "Object A = something;" as
implicit initialization. But I found a workaround for my problem:
giving the constructor a second argument with default initialization
and restricting the availability of the constructor by SFINAE for the
second argument. Thus I can explicitely control for wich
instantiations the constructor is available and for which not.
Thanks for your help.

regards,
Alex


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

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