VC++2010新手入门 联系客服

发布时间 : 星期四 文章VC++2010新手入门更新完毕开始阅读

using namespace std;

template void doshow( T t ) {

cout<

#define Show( test ) \\ { \\

cout<< #test<<\auto t = test; \\ doshow(t); \\ }

extern \{

class Test2 { }t2; }

class Test3 {

public: void print() { } }t3;

namespace ttt {

class Test4 { }t4; }

void testprint( int, double, float, float*, double* ) { }

int main() {

Show( 2 ); Show( 3.0f ); Show( 3.0 ); Show( \

Show( std::string(\Show( L\

Show( std::wstring(L\Show( (const int*)0 ); Show( (const int* const)0 );

class Test1 {

}t1;

Show( t1 );

Show( t2 ); Show( t3 );

Show( ttt::t4 );

Show( &Test3::print );

Show( std::mem_fun(&Test3::print) ); Show( testprint );

Show( std::bind( testprint ) );

Show( std::function< void () >() ); Show( doshow );

return 0; }

运行的结果: 2 : int 3.0f : float 3.0 : double \

std::string(\,class std::allocator>

L\

std::wstring(L\wchar_t>,class std::allocator> (const int*)0 : int const *

(const int* const)0 : int const *

t1 : ?AVTest1@?L@?main@

t2 : class Test2 t3 : class Test3

ttt::t4 : class ttt::Test4

&Test3::print : void (__thiscall Test3::*)(void)

std::mem_fun(&Test3::print) : class std::mem_fun_t testprint : void (__cdecl*)(int,double,float,float *,double *)

std::bind( testprint ) : class std::tr1::_Bind_fty> >

std::function< void () >() : class std::tr1::function doshow : void (__cdecl*)(double)

可以看出只要你提供合法的初始值,那么它就可以为你推断出类型来。 当然像这样的代码是不能通过编译的:auto a;

因为auto关键字要求必须有初始值。

error C3531: “a”: 类型包含“auto”的符号必须具有初始值设定项

下面的文字来自MSDN:

C++ 标准为 auto 关键字定义了初始和修订的含义。在 Visual C++ 2010 之前,该关键字在自动存储类中声明变量,即具有局部生存期的变量。从 Visual C++ 2010 开始,该关键字从声明的初始化表达式中推导变量的类型。使用 /Zc:auto[-] 编译器选项可指示编译器使用 auto 关键字的初始或修订的含义。

/Zc:auto[-] 编译器选项指示编译器如何使用 auto 关键字来声明变量。如果指定默认选项 /Zc:auto,编译器从其初始化表达式中推导声明的变量的类型。如果指定 /Zc:auto-,编译器将该变量分配给自动存储类。 2010.6.24晚Patch:

感谢yangjian8915提醒,你说的那个用法确实更能体现它的价值,不过另外还要谢谢你这个提醒让我想到一个我曾经遇到的问题,与大家分享,下面是我在做wxWidgets的时候遇到的问题: #include using namespace std; #define WXUSINGDLL #include int main() {

wxArrayString aryTest; aryTest.push_back(wxT(\

for( wxArrayString::iterator it = 0; it

if( (*it).CmpNoCase( wxT(\ {

// do sthing } } return 0; }

我的平台是VC2008,这是当时的一段出错的代码,编译器在编译时并未报错。造成错误的原因是粉色背景的那一句,之所以会有这样的代码是因为最初我想用下标来访问,后来又换成了迭代器,但是初始值却忘了改。这样的代码一运行必然会出错。

yangjian8915同学的留言让我猛然醒悟,如果用auto在编译时就能得到错误提示或者是警告,因此我改动了一下放到VC2010下面: #include using namespace std; #define WXUSINGDLL #include int main()

{

wxArrayString aryTest; aryTest.push_back(wxT(\

for( /*wxArrayString::iterator*/auto it = 0; it

if( (*it).CmpNoCase( wxT(\ {

// do sthing } } return 0; }

结果我得到一个编译错误:

main.cpp(13): error C2446: “<”: 没有从“wxArrayString::iterator”到“int”的转换 没有使该转换得以执行的上下文