就在他的blog上贴着。看最后一段
In the original language design, there was no explicit support for the param array that C# supports. Instead, one flags an ordinary array with an attribute, as follows:
void Trace1( String* format, [ParamArray]Object* args[] );
void Trace2( String* format, Object* args[] );
While these both look the same, the ParamArray attribute tags this for C# or other .NET languages as an array taking a variable number of elements with each invocation. In C++/CLI, the design for directly supporting this looked as follows:
void Trace1( String^ format, ... array<Object^>^ args );
void Trace2( String^ format, array<Object^>^ args );
in which the ellipsis […] preceding the array declaration tags it as a param array. Unfortunately, this has recently failed to make the list of features to be implemented for the inaugural release of C++/CLI. However, I can’t bear to [at least as yet] remove it from the translation tool.
看来问题在于自己看的文档是草案啊,手头的编译器可能还没能实现所有!!!
// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
using namespace System;
using namespace stdcli::language;
void F([ParamArray] array<int>^ args)
{
Console::WriteLine("# of arguments: {0}", args->Length);
for (int i = 0; i < args->Length; i++)
Console::WriteLine( "\targs[{0}] = {1}", i, args[i]);
}
void G([ParamArray] array<Object^>^ args)
{
Console::WriteLine("# of arguments: {0}", args->Length);
for (int i = 0; i < args->Length; i++)
Console::WriteLine( "\targs[{0}] = {1}", i, args[i]);
}
int _tmain()
{
// TODO: Please replace the sample code below with your own.
F();
F(1);
F(1, 2);
F(1, 2, 3);
);
G(10, "Hello", 1.23, 'X');
}
OK了!
但你用的还是比较老的语法,这样不会让人感到愉快。
再google,找到了这个"Visual C++ 2005 Tools Refresh" http://www.microsoft.com/downloads/details.aspx?FamilyID=afd04ff1-9d16-439a-9a5e-e13eb0341923&displaylang=en 。下载后会有vcpp_40904_install.exe文件 ,安装它。先编译使用[ParamArray]语法的,呵会看到下面的提示
warning C4572: [ParamArray] attribute is deprecated under /clr, use '...' instead
这时再次编译draft1.5的代码,这次可以了。 一切正常!
实际上在没有更新前编译器前, draft 1.5中其实有很多的内容是编译不过的。 例如在 8.14 Attributes 这一节
Type^ type1 = Class1::typeid;
这个原来就不能被编译通过,要改成
Type^ type = typeid<Class1>;
//或
Class1^ c = gcnew Class1();
Type^ type = c->GetType();
才能通过编译,升级编译器后这些问题也都被解决掉。
请发表评论