我想避免声明一个函数! [英] I want to avoid declaring a function!

查看:48
本文介绍了我想避免声明一个函数!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在解析返回语句的问题,以返回

函数(我认为)。点击此处:


模板< typename T> A类{};


模板< typename T> class maker_of_A {

public:

A< T>& make_A(){return A< T>();};

};


模板类maker_of_A< float> ;;


这个例子给出了错误:


typename.cc:在成员函数`A< T>& maker_of_A< T> :: make_A()[有T

=

浮动]'':

typename.cc:15:实例化自这里

typename.cc:11:无法将`{}''转换为'A< float>&''


我只想返回对匿名(临时)A< T>

对象的引用。我可以在不创建明确的临时对象的情况下这样做吗?

KCC做了我想要的而不抱怨,所以我从来没有想过

之前......(我知道这个例子没有意义,它适用于

显示我正在拥有的问题。)


谢谢,


/ Patrik

Hi,
I''m having a problem with a return statement being parsed to return a
function (I think). Check here:

template <typename T> class A {};

template <typename T> class maker_of_A {
public:
A<T>& make_A() {return A<T>();};
};

template class maker_of_A<float>;

This example gives the error:

typename.cc: In member function `A<T>& maker_of_A<T>::make_A() [with T
=
float]'':
typename.cc:15: instantiated from here
typename.cc:11: could not convert `{}'' to `A<float>&''

I just want to return a reference to an anonymous (temporary) A<T>
object. Can I do this without creating an explicit temporary object?
KCC did what I wanted without complaining, so I''ve never thought about
this before... (I know the example doesn''t make sense, it''s adapted to
show the issue I''m having.)

Thanks,

/Patrik

推荐答案

* lutorm:
我'我遇到一个问题,一个返回语句被解析为返回一个
函数(我认为)。点击此处:

模板< typename T> A类{};

模板< typename T> class maker_of_A {
公开:
A< T>& make_A(){return A< T>();};
};


返回const引用临时是未定义的行为;

返回非const引用,就像这里一样,不应该编译。


函数返回时临时已被销毁。


您将返回对不存在的对象的引用。


模板类maker_of_A< float> ;;

这个例子给出了错误:

typename.cc:在成员函数`A< T>& maker_of_A< T> :: make_A()[with T
=
float]'':
typename.cc:15:从这里实例化
typename.cc:11:could不要将`{}''转换为'A< float>&''

我只想返回对匿名(临时)A< T>
对象的引用。


无法做到。


您可以做的事情就像


#include <存储器GT&; // std :: auto_ptr

A级
{

私人:

A() ;

public:

static std :: auto_ptr< A>实例()

{

返回std :: auto_ptr< A>(新A);

}

};


int main()

{

std :: auto_ptr< A> p = A :: instance();

}


我可以在不创建显式临时对象的情况下执行此操作吗?


不,没有它,没有它。


KCC做了我想要的而不抱怨
I''m having a problem with a return statement being parsed to return a
function (I think). Check here:

template <typename T> class A {};

template <typename T> class maker_of_A {
public:
A<T>& make_A() {return A<T>();};
};
Returning const reference to temporary is undefined behavior;
returning non-const reference, as here, shouldn''t compile.

The temporary is already destroyed when the function returns.

You''re returning a reference to a non-existent object.

template class maker_of_A<float>;

This example gives the error:

typename.cc: In member function `A<T>& maker_of_A<T>::make_A() [with T
=
float]'':
typename.cc:15: instantiated from here
typename.cc:11: could not convert `{}'' to `A<float>&''

I just want to return a reference to an anonymous (temporary) A<T>
object.
No can do.

What you can do instead is something like

#include <memory> // std::auto_ptr

class A
{
private:
A();
public:
static std::auto_ptr<A> instance()
{
return std::auto_ptr<A>( new A );
}
};

int main()
{
std::auto_ptr<A> p = A::instance();
}

Can I do this without creating an explicit temporary object?
No, not with it, not without it.

KCC did what I wanted without complaining




大概是没有。


-

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

问:为什么这么糟糕?

A:热门发布。

问:什么是最烦人的事情usenet和电子邮件?



Presumably it didn''t.

--
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?


我*知道*返回非const引用是坏的。但它应该是

可能,不是吗?我应该收到关于将参考资料

返回临时的警告...如果我用
替换make_A的定义

A< T>& make_A(){A< T>温度;返回temp;};


我得到了我的预期:


typename.cc:在成员函数`A< T>& maker_of_A< T> :: make_A()[有T

=

浮动]'':

typename.cc:15:实例化自这里

typename.cc:12:警告:引用局部变量`temp''返回


但无论如何,这只是一个例子来说明什么没有b $ b工作。我实际上并不想通过引用返回,我想返回

值...好的,这是一个更真实的例子:


#include< memory>


模板< typename T> A类{

std :: auto_ptr< T> a;

};


模板< typename T> class maker_of_A {

public:

A< T>& make_A(){return A< T>();};

// A< T> make_A(){return A< T>();}; // alternate 1

// A< T>& make_A(){A< T>温度; return temp;}; // alternate 2

// A< T> make_A(){A< T>温度; return temp;}; // alternate 3

};


模板类maker_of_A< float> ;;


如上所列,这个例子给出错误:


typename.cc:在成员函数`A< T>& maker_of_A< T> :: make_A()[有T

=

浮动]'':

typename.cc:15:实例化自这里

typename.cc:9:无法将`A< float>()''转换为'A< float>&''


这里看起来返回尝试返回一个函数...

alternate 2给出:


typename.cc:在成员函数`A< T>中&安培; maker_of_A< T> :: make_A()[有T

=

浮动]'':

typename.cc:15:实例化自这里

typename.cc:11:警告:引用本地变量`temp''返回


好​​的,我很失望。但是,备用1,这就是我实际想要做的事情,给我带来完全难以理解的错误:


typename.cc:在成员函数中` A< T> maker_of_A< T> :: make_A()[有T =


浮动]'':

typename.cc:15:从这里实例化

typename.cc:10:没有匹配的函数来调用

`A< float> :: A(A< float>)''

typename .cc:3:候选者是:A< float> :: A(A< float>&)


它说它正在寻找一个按值调用的副本构造函数,但afaik

这是胡说八道,这样的事情并不存在。替代3似乎工作,

和我想知道的是如何写替代1使其工作像

替代3.

谢谢,


/ Patrik

I *know* returning the non-const reference is bad. But it should be
possible, no? I should just get a warning about returning a reference
to a temporary... If I replace the definition of make_A with

A<T>& make_A() {A<T> temp; return temp;};

I get what I expected:

typename.cc: In member function `A<T>& maker_of_A<T>::make_A() [with T
=
float]'':
typename.cc:15: instantiated from here
typename.cc:12: warning: reference to local variable `temp'' returned

But in any case, it was just an example to illustrate what doesn''t
work. I don''t actually want to return by reference, I want to return by
value... Ok, here''s a more true example:

#include <memory>

template <typename T> class A {
std::auto_ptr<T> a;
};

template <typename T> class maker_of_A {
public:
A<T>& make_A() {return A<T>();};
//A<T> make_A() {return A<T>();}; // alternate 1
//A<T>& make_A() {A<T> temp; return temp;}; // alternate 2
//A<T> make_A() {A<T> temp; return temp;}; // alternate 3
};

template class maker_of_A<float>;

As listed, this example gives the error:

typename.cc: In member function `A<T>& maker_of_A<T>::make_A() [with T
=
float]'':
typename.cc:15: instantiated from here
typename.cc:9: could not convert `A<float>()'' to `A<float>&''

Here it looks like the return tries to return a function... The
alternate 2 gives:

typename.cc: In member function `A<T>& maker_of_A<T>::make_A() [with T
=
float]'':
typename.cc:15: instantiated from here
typename.cc:11: warning: reference to local variable `temp'' returned

Ok, I''m down with that. But, alternate 1, which is what I actually want
to do, gives the to me completely incomprehensible error:

typename.cc: In member function `A<T> maker_of_A<T>::make_A() [with T =

float]'':
typename.cc:15: instantiated from here
typename.cc:10: no matching function for call to
`A<float>::A(A<float>)''
typename.cc:3: candidates are: A<float>::A(A<float>&)

It says it''s looking for a call-by-value copy constructor, but afaik
that''s nonsense, such a thing doesn''t exist. Alternate 3 seems to work,
and what I''m wondering is how to write alternate 1 to make it work like
alternate 3.

Thanks,

/Patrik


实际上,让我添加一些东西。如果我在最后添加这个,即使使用上面的备选3也不会有
工作:


maker_of_A< float>毫安;

A< float> aa = mA.make_A();


然后我得到了和以前一样奇怪的复制构造函数错误,现在最后一行是

。并且,不幸的是,这实际上是我的代码看起来像

之类的,所以即使使用备用3我也无法编译它。


/ Patrik

Actually, let me add something. Even using alternate 3 above doesn''t
work if I add this at the end:

maker_of_A<float> mA;
A<float> aa=mA.make_A();

Then I get the same strange copy constructor error as before, now at
the last line. And, unfortunately, that''s actually what my code looks
like, so even using alternate 3 I can''t get it to compile.

/Patrik


这篇关于我想避免声明一个函数!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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