这段代码有效吗? [英] is this code valid?

查看:81
本文介绍了这段代码有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码与GCC,Comeau,EDG / C ++和VC ++ 8/9编译良好:

#include< cstdio>

#include< string>

int main(){

{

std :: string names [3] = {" One"," Two", "三" };

}

std :: puts(" \ n\\\
press< ENTER to exit ...");

std :: getchar();

返回0;

}

我认为它仍然无效;我是对的吗?

-

Chris M. Thomasson
http://appcore.home.comcast.net

The following code compiles fine with GCC, Comeau, EDG/C++ and VC++ 8/9:
#include <cstdio>
#include <string>
int main() {
{
std::string names[3] = { "One", "Two", "Three" };
}
std::puts("\n\npress <ENTERto exit...");
std::getchar();
return 0;
}
I think its still invalid; am I right?
--
Chris M. Thomasson
http://appcore.home.comcast.net

推荐答案

Chris Thomasson写道:
Chris Thomasson wrote:

以下代码与GCC,Comeau,EDG / C ++和VC ++ 8/9编译良好:


#include< cstdio> ;

#include< string>


int main(){

{

std :: string names [3] = {" One"," Two"," Three" };

}

std :: puts(" \ n\\\
press< ENTER to exit ...");

std :: getchar();

返回0;

}


我认为它仍然无效;我对吗?
The following code compiles fine with GCC, Comeau, EDG/C++ and VC++ 8/9:
#include <cstdio>
#include <string>
int main() {
{
std::string names[3] = { "One", "Two", "Three" };
}
std::puts("\n\npress <ENTERto exit...");
std::getchar();
return 0;
}
I think its still invalid; am I right?



我认为你是对的,你认为它仍然无效(虽然有没有b $ b,但是你可以确认到目前为止,除非你告诉我们,否则我们无法知道你的想法。但是,我不会立即看到代码中的任何内容无效,你能否详细说明一下?


V

- -

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

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

I believe you''re right that you think it''s still invalid (although there
is nobody but you that can confirm that so far, there is no way for us
to know what you think except when you tell us). However, I don''t
immediately see anything invalid in the code, could you perhaps elaborate?

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


Victor Bazarov写道:
Victor Bazarov wrote:

Chris Thomasson写道:
Chris Thomasson wrote:

>以下代码与GCC,Comeau,EDG / C ++和VC ++ 8/9编译良好:

#include< cstdio>
#include< string>

int main(){
{
std :: string names [3] = {" One"," Two"," Three" std :: puts(" \ n\\\
press< ENTER to exit ...");
std :: getchar();
返回0;
}

我认为它仍然无效;我对吗?
>The following code compiles fine with GCC, Comeau, EDG/C++ and VC++ 8/9:
#include <cstdio>
#include <string>
int main() {
{
std::string names[3] = { "One", "Two", "Three" };
}
std::puts("\n\npress <ENTERto exit...");
std::getchar();
return 0;
}
I think its still invalid; am I right?



我相信你是对的,你认为它仍然是无效的(虽然有没有b $ b,但是你可以确认到目前为止,除非你告诉我们,否则我们无法知道你的想法。但是,我不会立即看到代码中的任何内容无效,你能否详细说明?


I believe you''re right that you think it''s still invalid (although there
is nobody but you that can confirm that so far, there is no way for us
to know what you think except when you tell us). However, I don''t
immediately see anything invalid in the code, could you perhaps elaborate?



该代码中有很多错误。对于初学者来说,它没有b $ b检查打印到标准输出是否成功。


(是的,这应该是幽默的。)

There are lots of things wrong in that code. For starters, it doesn''t
check that printing to standard output succeeded.

(Yes, that was supposed to be humoristic.)


" Juha Nieminen" < no **** @ thanks.invalidwrote in message

news:yW *************** @ read4.inet.fi ...
"Juha Nieminen" <no****@thanks.invalidwrote in message
news:yW***************@read4.inet.fi...

Victor Bazarov写道:
Victor Bazarov wrote:

> Chris Thomasson写道:
>Chris Thomasson wrote:

>>以下代码与GCC,Comeau,EDG / C ++和VC ++ 8/9编译良好:

#include< cstdio>
#include< ; string>

int main(){
{
std :: string names [3] = {" One"," Two"" Three" std :: puts(" \ n\\\
press< ENTER to exit ...");
std :: getchar();
返回0;
}

我认为它仍然无效;我对吗?
>>The following code compiles fine with GCC, Comeau, EDG/C++ and VC++ 8/9:
#include <cstdio>
#include <string>
int main() {
{
std::string names[3] = { "One", "Two", "Three" };
}
std::puts("\n\npress <ENTERto exit...");
std::getchar();
return 0;
}
I think its still invalid; am I right?


我相信你是对的,你认为它仍然无效(尽管除了你之外没有人可以证实到目前为止,没有办法为了我们
知道你的想法,除非你告诉我们)。但是,我不会立即在代码中看到任何无效的内容,您是否可以详细说明?


I believe you''re right that you think it''s still invalid (although there
is nobody but you that can confirm that so far, there is no way for us
to know what you think except when you tell us). However, I don''t
immediately see anything invalid in the code, could you perhaps
elaborate?



该代码中存在许多错误。对于初学者来说,它并不是b $ b检查打印到标准输出是否成功。


(是的,那应该是幽默的。)


There are lots of things wrong in that code. For starters, it doesn''t
check that printing to standard output succeeded.

(Yes, that was supposed to be humoristic.)



大声笑!


:^ D

好​​的......这就是我想要的去做;以下应编译

并运行正常:

__________________________________________________ _____________________

/ *活动对象

__________________________________________________ ____________ * /

模板<

typename T,

void(T :: * T_fp_start)()=& T :: start,

void(T :: * T_fp_join)()=& T :: join

lol!

:^D
Okay... Here is what I want to be able to do; the following should compile
and run fine:
__________________________________________________ _____________________
/* Active Object
__________________________________________________ ____________*/
template<
typename T,
void (T::*T_fp_start) () = &T::start,
void (T::*T_fp_join) () = &T::join


struct active {
struct active {



T object;


struct guard {

T&对象;


guard(T& _object):object(_object){

(object。* T_fp_start)();

}


~guard(){

(对象。* T_fp_join)();

}

};


有效(){

(对象。* T_fp_start)();

}


模板< typename T_p1>

有效(T_p1& p1):对象(p1){

(对象。* T_fp_start)() ;

}


模板< typename T_p1>

有效(T_p1 const& p1):object(p1){

(对象。* T_fp_start)();

}


模板< typename T_p1,typename T_p2>

有效(T_p1& p1,T_p2& p2):对象(p1,p2){

(对象。* T_fp_start)();

}


模板< typename T_p1,typename T_p2>

有效(T_p1 const& p1,T_p2& p2):object(p1,p2){

(对象。* T_fp_start)();

}


模板< typename T_p1,typename T_p2>

有效(T_p1& p1,T_p2 const& p2):object(p1,p2){

(object。* T_fp_start)();

}


template< typename T_p1,typename T_p2>

active(T_p1 const& p1,T_p2 const& p2):object(p1,p2){

(object。* T_fp_start)() ;

}


模板< typename T_p1,typename T_p2,typename T_p3>

有效(T_p1 const& p1,T_p2& p2,T_p3& p3)

:对象(p1,p2,p3){

(对象。* T_fp_start)();

}


// [以及更多和更多参数的开启......]


~active(){

(对象。* T_fp_join)();

}

};



/ *样本用法

__________________________________________________ ____________ * /

#include< cstdio>

#include< cstddef>

# include< string>


struct Buffer {

Buffer(){

std :: printf("(%p ) - > Buffer :: Buffer()\ n",(void *)这个);

}


~缓冲区(){

std :: printf("(%p) - > Buffer :: ~Buffer()\ n",(void *)this);

}

};

struct制作人{

int const m_id;

std :: string const m_name;

Buffer& m_Buffer;


生产者(int const id,Buffer& _Buffer)

:m_id(id),m_name(" Default Producer"),m_Buffer(_Buffer ){

std :: printf("(%p) - > Producer :: Producer()\ n",(void *)this);

} $ / $

生产者(int const id,char const * name,Buffer& _Buffer)

:m_id(id),m_name(name),m_Buffer(_Buffer ){

std :: printf("(%p) - > Producer :: Producer()\ n",(void *)this);

}


~Producer(){

std :: printf("(%p) - > Producer :: ~Producer()\ n" ;,(void *)this);

}


void start(){

std :: printf(" (%p)-void Producer<''%s''> :: start() - Buffer(%p)\ n",

(void *)this,m_name.c_str( ),(void *)& m_Buffer);

}


void join(){

std :: printf( "(%p)-void Producer :: join() - Buffer(%p)\ n",

(void *)this,(void *)& m_Buffer); < br $>
}

};

struct Consumer {

int const m_id;

std :: string const m_name;

Buffer& m_Buffer;


Consumer(int const id,Buffer& _Buffer)

:m_id(id),m_name(" Default Consumer"),m_Buffer(_Buffer ){

std :: printf("(%p) - > Consumer :: Consumer()\ n",(void *)this);

} $ / $

Consumer(int const id,char const * name,Buffer& _Buffer)

:m_id(id),m_name(name),m_Buffer(_Buffer ){

std :: printf("(%p) - > Consumer :: Consumer()\ n",(void *)this);

}


~Consumer(){

std :: printf("(%p) - > Consumer :: ~Finomers()\ n" ;,(void *)this);

}


void start(){

std :: printf(" (%p)-void Consumer<''%s''> :: start() - Buffer(%p)\ n",

(void *)this,m_name.c_str( ),(void *)& m_Buffer);

}


void join(){

std :: printf( "(%p)-void Consumer :: join() - Buffer(%p)\ n",

(void *)this,(void *)& m_Buffer); < br $>
}

};

#define ARRAY_DEPTH(mp_this)(\

sizeof((mp_this))/ sizeof((mp_this)[0])\



int main(){

{

缓冲区b;


有效< Producerp [] = {

有效< Producer>(123,b),

有效<制片人>(456,Custom Producer,b),

有效<制片人>(789,b),

有效<制片人>(234," I我是制片人!",b),

有效<制片人>(567,b)

};


有效< ; Consumerc [] = {

有效<消费者>(891,我是消费者!,b),

有效<消费者>(345,b ),

有效<消费者>(678,b)

};


std :: size_t i;


std :: puts(" -----------------------");

for(i = 0;我< ARRAY_DEPTH(P); ++ i){

std :: printf(" p [%u] .m_id ==%d \ n",i,p [i] .object.m_id);

std :: printf(" p [%u] .m_name ==%s \ n ---- \ n",

i,p [i]。 object.m_name.c_str());

}


putchar(''\ n'');


for(i = 0; i< ARRAY_DEPTH(c); ++ i){

std :: printf(" c [%u] .m_id ==%d \ n",i,c [i] .object.m_id);

std :: printf(" c [%u] .m_name ==%s \ n ---- \ n",

i,c [i] .object.m_name.c_str());

}


std :: puts(" -----------------------");

}

std: :puts(" \\\
\\\
press< ENTER to exit ...");

std :: getchar();

返回0;

}


__________________________________________________ _____________________


此代码编译完全没有GCC,Comeau,VC ++ 8/9的警告

和EDG / C ++。

你们中的任何人都可以发现任何一个精炼的行为?此外,代码是否接近

标准C ++?请尝试忍受在这里。我不是C ++专家...


:^(

感谢您的所有时间!我真的很感激。

T object;

struct guard {
T& object;

guard(T& _object) : object(_object) {
(object.*T_fp_start)();
}

~guard() {
(object.*T_fp_join)();
}
};

active() {
(object.*T_fp_start)();
}

template<typename T_p1>
active(T_p1& p1) : object(p1) {
(object.*T_fp_start)();
}

template<typename T_p1>
active(T_p1 const& p1) : object(p1) {
(object.*T_fp_start)();
}

template<typename T_p1, typename T_p2>
active(T_p1& p1, T_p2& p2) : object(p1, p2) {
(object.*T_fp_start)();
}

template<typename T_p1, typename T_p2>
active(T_p1 const& p1, T_p2& p2) : object(p1, p2) {
(object.*T_fp_start)();
}

template<typename T_p1, typename T_p2>
active(T_p1& p1, T_p2 const& p2) : object(p1, p2) {
(object.*T_fp_start)();
}

template<typename T_p1, typename T_p2>
active(T_p1 const& p1, T_p2 const& p2) : object(p1, p2) {
(object.*T_fp_start)();
}

template<typename T_p1, typename T_p2, typename T_p3>
active(T_p1 const& p1, T_p2& p2, T_p3& p3)
: object(p1, p2, p3) {
(object.*T_fp_start)();
}

// [and on and on for more and more parameters...]

~active() {
(object.*T_fp_join)();
}
};


/* Sample usage
__________________________________________________ ____________*/
#include <cstdio>
#include <cstddef>
#include <string>

struct Buffer {
Buffer() {
std::printf("(%p)->Buffer::Buffer()\n", (void*)this);
}

~Buffer() {
std::printf("(%p)->Buffer::~Buffer()\n", (void*)this);
}
};
struct Producer {
int const m_id;
std::string const m_name;
Buffer& m_Buffer;

Producer(int const id, Buffer& _Buffer)
: m_id(id), m_name("Default Producer"), m_Buffer(_Buffer) {
std::printf("(%p)->Producer::Producer()\n", (void*)this);
}

Producer(int const id, char const* name, Buffer& _Buffer)
: m_id(id), m_name(name), m_Buffer(_Buffer) {
std::printf("(%p)->Producer::Producer()\n", (void*)this);
}

~Producer() {
std::printf("(%p)->Producer::~Producer()\n", (void*)this);
}

void start() {
std::printf("(%p)-void Producer<''%s''>::start() - Buffer(%p)\n",
(void*)this, m_name.c_str(), (void*)&m_Buffer);
}

void join() {
std::printf("(%p)-void Producer::join() - Buffer(%p)\n",
(void*)this, (void*)&m_Buffer);
}
};
struct Consumer {
int const m_id;
std::string const m_name;
Buffer& m_Buffer;

Consumer(int const id, Buffer& _Buffer)
: m_id(id), m_name("Default Consumer"), m_Buffer(_Buffer) {
std::printf("(%p)->Consumer::Consumer()\n", (void*)this);
}

Consumer(int const id, char const* name, Buffer& _Buffer)
: m_id(id), m_name(name), m_Buffer(_Buffer) {
std::printf("(%p)->Consumer::Consumer()\n", (void*)this);
}

~Consumer() {
std::printf("(%p)->Consumer::~Consumer()\n", (void*)this);
}

void start() {
std::printf("(%p)-void Consumer<''%s''>::start() - Buffer(%p)\n",
(void*)this, m_name.c_str(), (void*)&m_Buffer);
}

void join() {
std::printf("(%p)-void Consumer::join() - Buffer(%p)\n",
(void*)this, (void*)&m_Buffer);
}
};
#define ARRAY_DEPTH(mp_this) ( \
sizeof((mp_this)) / sizeof((mp_this)[0]) \
)
int main() {
{
Buffer b;

active<Producerp[] = {
active<Producer>(123, b),
active<Producer>(456, "Custom Producer", b),
active<Producer>(789, b),
active<Producer>(234, "I am a Producer!", b),
active<Producer>(567, b)
};

active<Consumerc[] = {
active<Consumer>(891, "I am a Consumer!", b),
active<Consumer>(345, b),
active<Consumer>(678, b)
};

std::size_t i;

std::puts("-----------------------");

for (i = 0; i < ARRAY_DEPTH(p); ++i) {
std::printf("p[%u].m_id == %d\n", i, p[i].object.m_id);
std::printf("p[%u].m_name == %s\n----\n",
i, p[i].object.m_name.c_str());
}

putchar(''\n'');

for (i = 0; i < ARRAY_DEPTH(c); ++i) {
std::printf("c[%u].m_id == %d\n", i, c[i].object.m_id);
std::printf("c[%u].m_name == %s\n----\n",
i, c[i].object.m_name.c_str());
}

std::puts("-----------------------");
}
std::puts("\n\npress <ENTERto exit...");
std::getchar();
return 0;
}

__________________________________________________ _____________________

This code compiles perfectly fine with no warnings on GCC, Comeau, VC++ 8/9
and EDG/C++.
Can any of you spot any undefined behavior? Also, is the code anywhere near
standard C++? Please try and bear with be here. I am NOT a C++ expert...

:^(
Thanks for all of your time! I really do appreciate it.


这篇关于这段代码有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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