V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
iambic
V2EX  ›  问与答

继续问一个 C++里的模板问题

  •  
  •   iambic · 2015-10-08 21:27:06 +08:00 · 2032 次点击
    这是一个创建于 3136 天前的主题,其中的信息可能已经有所发展或是发生改变。

    主要是 is_constructible 的使用问题

    class Json{
        // Implicit constructor: map-like objects (std::map, std::unordered_map, etc)
        template <class M, typename std::enable_if<
            std::is_constructible<std::string, typename M::key_type>::value
            && std::is_constructible<Json, typename M::mapped_type>::value,
                int>::type = 0>
        Json(const M & m) : Json(object(m.begin(), m.end())) {}
    
        // Implicit constructor: vector-like objects (std::list, std::vector, std::set, etc)
        template <class V, typename std::enable_if<
            std::is_constructible<Json, typename V::value_type>::value,
                int>::type = 0>
        Json(const V & v) : Json(array(v.begin(), v.end())) {}
        ...
    };
    

    这两个构造函数,第一个是说,接受一个类似 map 的东西。这个我大概能看明白
    第一个 is_contructible 是说, class M 里边需要定义 M::key_type 这种类型,而且这种类型可以用来初始化 std::string
    即 std::string(M::key_type), 显然 M::key_type 只能是 std::string 或者 const char*
    第二个类似,要求 class M 里边还要定义 M::mapped_type ,并且这种类型可以用来初始化 Json, 根据之前的定义,这种 M::mapped_type 包括 string, array, object, boolean 等等。

    总之,这样的 M 大概就是类似 map 或者 unordered_map 这个样子。 key 是 string , value 是其他可以用来初始化 Json 的类型。

    但是第二个构造函数就看不太明白了,看注释是说,接受一个类似 vector 的对象,包括 list,vector,set 等。

    std::is_constructible<Json, typename V::value_type>::value,int>
    

    这个应该是说, class V 首先内部需要有一个 value_type 的类型,同时 value_type 和 int 可以用来共同初始化 Json ,即要求
    Json(V::value_type, int)这样的构造函数是合法的。

    为了让问题明确,我把 class Json 所有的构造函数全部列了出来,似乎没有看到哪个构造函数有
    Json(V::value_type, int)这样的形式。。

    那上边第二个构造函数,到底是怎么来要求,输入的 class V 是 vector 类似的类型的呢?
    谢谢!

    附,以下是 class Json 所有的构造函数。

    class Json final {
    public:
        // Types
        enum Type {
            NUL, NUMBER, BOOL, STRING, ARRAY, OBJECT
        };
    
        // Array and object typedefs
        typedef std::vector<Json> array;
        typedef std::map<std::string, Json> object;
    
        // Constructors for the various types of JSON value.
        Json() noexcept;                // NUL
        Json(std::nullptr_t) noexcept;  // NUL
        Json(double value);             // NUMBER
        Json(int value);                // NUMBER
        Json(bool value);               // BOOL
        Json(const std::string &value); // STRING
        Json(std::string &&value);      // STRING
        Json(const char * value);       // STRING
        Json(const array &values);      // ARRAY
        Json(array &&values);           // ARRAY
        Json(const object &values);     // OBJECT
        Json(object &&values);          // OBJECT
    
        // Implicit constructor: anything with a to_json() function.
        template <class T, class = decltype(&T::to_json)>
        Json(const T & t) : Json(t.to_json()) {}
    
        // Implicit constructor: map-like objects (std::map, std::unordered_map, etc)
        template <class M, typename std::enable_if<
            std::is_constructible<std::string, typename M::key_type>::value
            && std::is_constructible<Json, typename M::mapped_type>::value,
                int>::type = 0>
        Json(const M & m) : Json(object(m.begin(), m.end())) {}
    
        // Implicit constructor: vector-like objects (std::list, std::vector, std::set, etc)
        template <class V, typename std::enable_if<
            std::is_constructible<Json, typename V::value_type>::value,
                int>::type = 0>
        Json(const V & v) : Json(array(v.begin(), v.end())) {}
    
        // This prevents Json(some_pointer) from accidentally producing a bool. Use
        // Json(bool(some_pointer)) if that behavior is desired.
        Json(void *) = delete;
    
    2 条回复    2015-10-09 10:58:43 +08:00
    hyq
        1
    hyq  
       2015-10-09 01:03:02 +08:00   ❤️ 1
    “同时 value_type 和 int 可以用来共同初始化 Json ,即要求 Json(V::value_type, int)这样的构造函数是合法的。”
    这个 int 哪来的,是不是 enable_if 的第二个参数?
    iambic
        2
    iambic  
    OP
       2015-10-09 10:58:43 +08:00
    @hyq 0 0 确实是我看错了

    int 确实应该是 enable_if 的第二个参数。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3648 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 00:46 · PVG 08:46 · LAX 17:46 · JFK 20:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.