How to get the default value of any type

后端 未结 4 1679
难免孤独
难免孤独 2021-02-08 07:47

In C# I can write something like this:

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

    static void Main ()
    {
              


        
相关标签:
4条回答
  • 2021-02-08 08:11

    ForEveR's answer will not work if T doesn't have a copy constructor. In C++03, there is no way to zero-initialize a variable that is both generic and elegant. All that's left is the following trick.

    T temp[1] = {};
    T & obj = temp[0];
    

    Here, temp[0] is zero-initialized and then bound to obj. No copy constructors are needed.

    0 讨论(0)
  • 2021-02-08 08:15

    Taken literaly from "The C++ Programming Language, Third Edition by Bjarne Stroustrup":

    BEGIN QUOTE

    4.9.5 Initialization [dcl.init]

    If an initializer is specified for an object, that initializer determines the initial value of an object. If no initializer is specified, a global (§4.9.4), namespace (§8.2), or local static object (§7.1.2, §10.2.4) (collectively called static objects) is initialized to 0 of the appropriate type. For example:

    int a;  // means int a=0;
    double d; // meands d=0;
    

    Local variables (sometimes called automatic objects) and objects created on the free store (sometimes called dynamic objects or heap objects) are not initialized by default. For example:

    void f()
    {
       int x;   // x does not have a well-defined value
       // . . . 
    }
    

    Members of arrays and structures are default initialized or not depending on whether the array or structure is static. User-defined types may have default initialization defined (§10.4.2).

    More complicated objects require more than one value as an initializer. This is handled by initializer lists delimited by { and } for C-style initialization of arrays (§5.2.1) and structures (§5.7).

    For user-defined types with constructors, function-style argument lists are used (§2.5.2, §10.2.3). Note that an empty pair of parentheses () in a declaration always means ‘‘function’’ (§7.1). For example:

    int a[] = {1,2};    // array initializer
    Point z(1,2);       // function-style initializer (initialization by constructor)
    int f();            // function declaration
    

    END QUOTE

    So, you can get the default value of any type form a static object of that type:

    static T defaultT; // `defaultT' has de default value of type T
    
    0 讨论(0)
  • 2021-02-08 08:17

    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;
    
    0 讨论(0)
  • 2021-02-08 08:33

    Create your own default keyword:

    class default_t
    {
    public:
      template<typename T>
      operator T() const { return T(); }
    };
    
    default_t const default = default_t();
    

    Use it like:

    int myInt = default;
    vector<string> myVector = default;
    shared_ptr<string> myPtr = default;
    

    Or with a slight semantic variation:

    default_t const empty = default_t();
    
    vector<Persons> fetchPersons()
    {
      if (Database::isConnected())
      {
        return Database::fetchPersons();
      }
    
      return empty;
    }
    
    0 讨论(0)
提交回复
热议问题