运营商... [英] Operator...

查看:60
本文介绍了运营商...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前可能已经提出过很多问题,但我只是想念它。


为什么没有像这样的东西操作员...()

imho它可以让我们平滑c +

+的一个更尖锐的角落。


你可以做这样的事情:


类字符串

{

public:


const char * operator ...()const

{

返回c_str();

}

};

void foo(const char * format,...)

{

//通常的vaarg东西

}

void bar()

{

string s =" test";


foo(这是一个测试%s,s);

}


只是一个快速思考,

M.

this has probably been asked/suggested many timtes before, but i just
thought of it.

why is there no such thing as something like operator...()
imho it would allow us to smooth off one of the sharper corners of c+
+.

you could maybe do something like this:

class string
{
public:

const char *operator...() const
{
return c_str();
}
};
void foo(const char *format, ...)
{
//the usual vaarg stuff
}
void bar()
{
string s = "test";

foo("this is a test %s", s);
}

just a quick thought,

M.

推荐答案

ma *********** @ hotmail.com 写道:

这可能是之前被问过/建议很多,但我只是想想它。$ / b

为什么没有像运算符这样的东西......()

imho它可以让我们平滑c +

+的一个更尖锐的角落。


你可以做这样的事情:


类字符串

{

公开:


const char * operator ...()const

{

返回c_str();

}

};


void foo(const char * format,...)

{

//通常的vaarg东西

}


void bar()

{

string s =" test" ;


foo(这是测试%s,s);

}


只是快速思考,
this has probably been asked/suggested many timtes before, but i just
thought of it.

why is there no such thing as something like operator...()
imho it would allow us to smooth off one of the sharper corners of c+
+.

you could maybe do something like this:

class string
{
public:

const char *operator...() const
{
return c_str();
}
};
void foo(const char *format, ...)
{
//the usual vaarg stuff
}
void bar()
{
string s = "test";

foo("this is a test %s", s);
}

just a quick thought,



嗯,乍一看它看起来还不错,但后来我想,所以为什么不能给你b
只需要


foo(这是一个测试%s ;,s.c_str());


?如果不改变

语言,它将完成完全相同的事情。你是否认为你可以通过使用static_cast<或已经存在

的成员函数来解决这个问题?




一般来说,你推广的是最低级别的转换运算符

,当没有其他标准或用户定义的转换时可以使用

适合,我猜。由于我不会使用那么多的转换,所以我不能立即看到其他地方(除了实际的''...'')

会很有用。


V

-

请在通过电子邮件回复时删除资金'A'

我没有回复最热门的回复,请不要问

Well, on the first glance it looked OK, but then I thought, so why can''t
you just do

foo("this is a test %s", s.c_str());

? It would accomplish the exactly same thing without changing the
language. Do you think you could give an example that couldn''t be
countered by a use of a static_cast<or a member function that already
exists?

Generally speaking, you''re promoting the lowest rank conversion operator
which can be used when no other standard or user-defined conversion is
suitable, I am guessing. Since I don''t use conversions that much I
can''t really immediately see where else (aside from the actual ''...'') it
would be useful.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


是的,标准的事情是必须打电话给s。 c_str()。

标准板故意没有将操作符char *放入

字符串中,原因是你无法使std :: string类行为

完全像一个pod类型,就像其他语言一样,因为变量

参数。


i意味着这只是作为一个类beign的可能转换通过

进入变量参数参数列表。它是其中一个让人们接受的东西,让其他程序员想知道为什么c ++不是什么时候

更有帮助。


只是觉得它可能是一个方便的小补充。


M.
yeah the standard thing is to have to call s.c_str().
the standards board purposefully didnt put an operator char* into
string for the reason that you cant make the std::string class act
entirely like a pod type like in other languages because of variable
arguments.

i meant this only as a possible conversion for a class beign passed
into a variable argument parameter list. its one of those things that
catches people out, and makes other programmers wonder why c++ isnt
more helpfull some times.

just thought it could be a handy little addition.

M.


ma *********** @ hotmail.com 写道:

之前可能有人问过/建议很多,但我只是想念它。


为什么没有这样的东西像操作员...()

imho它可以让我们平滑c +

+的一个更尖锐的角落。
this has probably been asked/suggested many timtes before, but i just
thought of it.

why is there no such thing as something like operator...()
imho it would allow us to smooth off one of the sharper corners of c+
+.



下一个C ++标准将引入可变参数模板,这将允许写入类型安全的函数,这些函数采用可变数量的

参数。我相信这将允许你做你所指的

到这里。

The next C++ standard will introduce variadic templates, which will
allow writing type-safe functions which take a variable amount of
parameters. I believe that will allow you to do what you are referring
to here.


这篇关于运营商...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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