c++c++11idiomsrule-of-zero

Trouble understanding the C++11 syntax in the Rule of Zero


I am studying the Rule of Zero and have 2 questions for the final piece of code which demonstrates the rule.

class module {
    public:
        explicit module(std::wstring const& name)
        : handle { ::LoadLibrary(name.c_str()), &::FreeLibrary } {}
    
        // other module related functions go here
    
    private:
        using module_handle = std::unique_ptr<void, decltype(&::FreeLibrary)>;
    
        module_handle handle;
    };
  1. Why a curly bracket usage instead of parentheses to initialize handle?
  2. What does using module_handle = std::unique_ptr<void, decltype(&::FreeLibrary)>; mean exactly in this context? would it be possible to replace it with a typedef?

Solution

  • Why a curly bracket usage instead of parentheses to initialize handle?

    It's uniform initializing method introduced in C++11

    What does using module_handle = std::unique_ptr; mean exactly in this context? would it be possible to replace it with a typedef?

    It's template aliasing and also for creating new type. using is a new and powerful replacement of typedef