如何获取任何类型的默认值 [英] How to get the default value of any type

查看:316
本文介绍了如何获取任何类型的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中我可以这样写:

  class AnyThing< T& 
{
static public T Default = default(T);
}

static void Main()
{
int i = AnyThing< int&
Console.WriteLine(i == 0);
string s = AnyThing< string> .Default;
Console.WriteLine(s == null);

}



我想在C ++中写一个像模板类的字典,我想要dict返回默认值(zero out)的通用TVal类型,如果给定的键找不到。在C#中,默认(T)构造可以解决,而在C ++中我不知道什么是适当的方式做同样的事情。



使用gcc4.7的T obj = {} T * obj = {} 我只是不确定如果它是由语言规范定义的语法,如果这种代码将是便携式交叉编译器和平台。请帮我我的doudt!提前感谢!



PS:



~~~~~~~~~~



为了确保模板获得ANY类型的默认(零输出)值,即使没有可调用的默认ctor,我采用以下机制(灵感来自avakar的答案) :

 模板< class T> 
struct AnyThing
{
static const T&默认;
private:
static const char temp [sizeof(T)];
};

template< class T> const char AnyThing< T> :: temp [] = {};
template< class T> const T& AnyThing< T> :: Default = *(T *)temp;

struct st
{
double data;
st()= delete;
};

int main()
{
cout< (int)AnyThing< char *> :: Default<< endl; // 0
cout<< AnyThing< int> :: Default<< endl; // 0
cout<< AnyThing< st> :: Default.data<< endl; // 0
}

看起来很丑陋,所有一个零化对象只是一个空白内存块。我在C ++中没有像 default 这样的东西。

关键字在C#。因为默认构造函数的类类型的初始化将失败,如果默认构造函数 private 。在C#中,如果默认构造函数是私有的,类类型的值将初始化为 null ,因为类类型引用类型



{} 初始化由语言规范定义。它是C ++ 11。在C ++ 03中,你应该使用

  T obj = T 

正如 bames53 在注释中指出,当您要初始化 T * 您应该在C ++ 11之前使用



  T * obj = 0; 

  T * obj = NULL; C ++ 11中的

  T * obj = {}; 

  T * obj = nullptr; 


In C# I can write something like this:

    class AnyThing<T>
    {
        static public T Default = default(T);
    }

    static void Main ()
    {
        int i = AnyThing<int>.Default;
        Console.WriteLine (i==0);
        string s = AnyThing<string>.Default;
        Console.WriteLine (s == null);

    }

I intend to write a dictionary like template class in C++, I'd like the dict to return the default value (zero out) of the generic TVal type if the given key not be found. In C# the default(T) construct comes to rescue, while in C++ I'm not sure what is the appropriate way to do the same thing.

I've tried T obj = {} and T* obj = {} with gcc4.7, it works well. I'm just not so sure if it is the syntax defined by the language specification, if this kinda code will be portable cross compilers and platforms. Please help me with my doudt! Thanks in advance!

PS:

~~~~~~~~~~

To make sure the template get the default(zero out) value of ANY type, even of those that don't have callable default ctor, I employed following mechanism (inspired by avakar's answer):

template<class T>
struct AnyThing
{
    static const T& Default ;
private:
    static const char temp[sizeof(T)];
};

template<class T> const char AnyThing<T>::temp[] = {};
template<class T> const T& AnyThing<T>::Default =  *(T*)temp;

struct st
{
    double data;
    st()=delete;
};

int main()
{
    cout << (int)AnyThing<char*>::Default<<endl;    //0
    cout << AnyThing<int>::Default<<endl;       //0
    cout <<AnyThing<st>::Default.data<<endl;        //0
}

It looks ugly, but shouldn't cause any trouble, after all a zeroed out object is just a chunk of blank memory. Am I wrong?

解决方案

In C++ there is no something like default keyword in C#. Since initialization by default constructor of value of class-type will be failed, if default constructor is private. In C#, if default constructor is private, value of class-type will be initialized to null, since class-type is reference-type.

Initialition by {} is defined by language specification. It's C++11. In C++03 you should use

T obj = T();

As pointed by bames53 in comment, when you want to initialize T* you should use

before C++11.

T* obj = 0;

or

T* obj = NULL;

in C++11.

T* obj = {};

or

T* obj = nullptr;

这篇关于如何获取任何类型的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆