3

次のコードを検討してください。

public ref class Factory
{
public:
    generic <typename T> where T : value class, System::ValueType
    static System::Nullable<T> Create()
    {
        return System::Nullable<T>();
    }
};

Visual C++ 2008 は次のエラーを吐き出します。

error C2440: 'return' : cannot convert from 'System::Nullable<T>' to 'System::Nullable<T>'

「System::Nullable」型をユーザー定義型に置き換えると、問題なく動作します。

generic <typename T> where T : value class, System::ValueType
public value class MyType 
{ };

public ref class Factory
{
public:
    generic <typename T> where T : value class, System::ValueType
    static MyType<T> Create()
    {
        return MyType<T>();
    }
};

これはある種の VC++ バグですか、それともここで何か不足していますか?

4

2 に答える 2

1

同じ問題が発生する可能性がある場合は、次の回避策を使用できます。

public ref class Factory
{
public:
    generic <typename T> where T : value class, System::ValueType
    static System::Nullable<T> Create()
    {
        return Workaround<T>::Nullable();
    }

private:
    generic <typename T> where T : System::ValueType, value class
    value struct Workaround {
        typedef System::Nullable<T> Nullable;
    };
};
于 2013-03-19T19:01:43.997 に答える
0

やってみました:

public ref class Factory
{
public:
    generic <typename T> where T : value class, System::ValueType
    static System::Nullable<T> Create()
    {
        return Null;
    }
};
于 2012-07-26T20:25:16.357 に答える