I'm trying to return a nullptr from a generic function in C++/CLI and I am receiving the following error:
error C2440: 'return' : cannot convert from 'nullptr' to 'T'
'T' may be a value type at runtime: consider using 'T()' instead
I wrote the following C# and C++ classes to test the difference. The code compiled and ran in C# but the code will not compile in C++/CLI. Here’s the C# code:
using System;
using System.Collections.Generic;
using System.Text;
namespace TestGenerics
{
class Program
{
static void Main( string[] args )
{
Something st = new Something();
string s = st.DoSomething<string>();
Console.WriteLine( "s = " + (s == null "null" : s ));
Console.ReadKey();
}
}
public class Something
{
public T DoSomething<T>() where T : class
{
return null;
}
}
}
Here’s the C++/CLI code:
using namespace System;
namespace CPPSomething {
public ref class Something
{
public:
generic <class T> where T : ref class
T DoSomething()
{
return nullptr;
}
};
}
So, my question is, do I have the wrong syntax in C++/CLI, is this not possible in C++/CLI, or is this a compiler bug which prevents recognizing our constraint