views:

187

answers:

1

Following up from my previous question.

Can anyone explain why the following code compiles without any errors:

typedef array<VdbMethodInfo^> MethodArray;
typedef array<VdbParameterInfo^> ParameterArray;
ParameterArray^ parameters = gcnew ParameterArray {
    gcnew VdbParameterInfo("name", "string", "Paul")};
MethodArray^ methods = gcnew MethodArray {
    gcnew VdbMethodInfo("createTable", parameters)
};

Yet this gives me "error C2440: 'initializing' : cannot convert from 'VdbParameterInfo ^' to 'VdbMethodInfo ^"

typedef array<VdbMethodInfo^> MethodArray;
typedef array<VdbParameterInfo^> ParameterArray;
MethodArray^ methods = gcnew MethodArray {
    gcnew VdbMethodInfo("createTable", gcnew ParameterArray {
        gcnew VdbParameterInfo("name", "string", "Paul")};
    )
};

All I've done is attempt to "nest" the parameter array inside the method array initialization... Not directly mind - VdbMethodInfo's constructor takes, as a second argument, a ParameterArray.

It seems to imply that managed C++ array initialization expects any recursive nesting to have the same type... (i.e. I think this must be a bug)

Related question : here

+2  A: 

I've found a workaround which makes the syntax cleaner anyway. I use the "..." syntax (Managed C++ equivalent to the C# "params" keyword"):

public ref class MetaData
{
 typedef array<VdbMethodInfo^> MethodArray;
 typedef array<VdbParameterInfo^> ParameterArray;
 static ParameterArray^ params(... ParameterArray^ p)
 {
  return p;
 }
public:
 static array<VdbMethodInfo^>^ Instance()
 {
  ParameterArray^ parameters = gcnew ParameterArray { gcnew VdbParameterInfo("name", "string", "Paul")};
  MethodArray^ methods = gcnew MethodArray {
   gcnew VdbMethodInfo("createTable",
    params(gcnew VdbParameterInfo("name", "string", "Paul"),
        gcnew VdbParameterInfo("age", "number", "25")))
  };

  return methods;
 }
};
Paul Hollingsworth