Zoli
Zoli

Reputation: 911

std::vector is not accepted as delegate return type in C++/CLI? C2065 C2061

I have the following C++/CLI snippet:

.h
#pragma unmanaged
#include <vector>

public delegate std::vector<std::wstring> XYZ(const std::wstring& filter);

.cpp

XYZ^ xyz = gcnew XYZ(&myClass::xyzFunc); // <-error C2065 + C2061

This case I get at this line two errors:

error C2065: 'xyz' : undeclared identifier

error C2061: syntax error : identifier 'XYZ'

However, if I change the delegate return type from vector -> wstring (for example), it works!

public delegate std::wstring XYZ(const std::wstring& filter); // <-- w/o vector<> , works!

Has anyone any idea what is the problem? Greatly appreciated!

Upvotes: 1

Views: 473

Answers (1)

Hans Passant
Hans Passant

Reputation: 942177

Clearly this is a compiler defect, it should at least have emitted a diagnostic why it didn't add the delegate type to the symbol table. You could submit it to connect.microsoft.com but they are not going to fix it.

A workaround is to use a typedef to declare the return value type:

typedef std::vector<std::wstring> returntype;
delegate returntype XYZ(const std::wstring& filter);

I would urge you a bit to treat C++/CLI as an interop language, its major reason for being. This delegate is not usable by any other managed code. Do favor String and List<String^> here.

Upvotes: 1

Related Questions