代码之家  ›  专栏  ›  技术社区  ›  Rick Jim DeLaHunt

STD:设置构造函数签名混淆

c++
  •  3
  • Rick Jim DeLaHunt  · 技术社区  · 7 年前

    我在看书 C++底漆 关于如何定义 set .当我看穿 CPP参考 关于 设置 ,我对类和构造函数签名感到困惑 设置 .

    从文本:

    bool compareIsbn(){....}
    multiset<Sales_data, decltype(compareIsbn) *> bookstore(compareIsbn)
    

    文本1:

    要使用我们自己的操作,我们必须用两种类型定义多集: 键类型, Sales_data 和比较类型,这是一个函数 可指向的指针类型 compareIsbn .当我们定义 这种类型,我们提供一个指向要使用的操作的指针。在 本例中,我们提供了一个指针 比较 以下内容:

    所以从text1来看,我需要 函数指针 在里面 set<Class A, a function pointer> .

    但是当我抬头看的时候 cppreference “签名”(不确定是否是正确的词)是:

    template<
        class Key,
        class Compare = std::less<Key>,
        class Allocator = std::allocator<Key>
    > class set;
    

    问题1 :所以第二个参数应该是 class 是吗?


    文本2:

    我们可以写信 比较 而不是 &compareIsbn 作为建造师 参数,因为当我们使用函数名时, 根据需要自动转换为指针。我们可以 书面 &比较 同样的效果。

    也来自 cppreference constructor of set (我认为课本使用的是第一个构造器):

    set();
    explicit set( const Compare& comp, 
                  const Allocator& alloc = Allocator() );
    

    问题2 :构造函数不需要 函数引用 是吗?从它说它需要一个 函数指针 是吗?


    需要一些帮助来理解“签名”,提前谢谢:d。

    附言:

    1. 教科书使用 multiset 例如,但是 设置 多集 非常相似。
    2. 我不知道什么 const Allocator& alloc = Allocator() 意思是,但我想这可能是自动完成的事情?我暂时可以忽略它。
    2 回复  |  直到 7 年前
        1
  •  3
  •   James Picone    7 年前

    template<typename T>
    struct Foo {
        T thing;
    }
    
    template<class T>
    struct Bar {
        T thing;
    }
    
    // Foo and Bar will work identically
    


    Compare

    template<typename T>
    void Foo(const T& object) { ... }
    

    Thing* bar = new Thing();
    Foo<Thing*>(bar); // This instantiation of the template has signature void (const Thing*&)
    

    decltype(CompareIsbn)* const decltype(CompareIsbn)*&


    page set

    // Given Compare comp
    // Given Key a, b
    bool b = comp(a, b);
    

    operator()

    std::function = std::less<Key> Key::operator<

    Allocator Key

        2
  •  1
  •   StoryTeller - Unslander Monica    7 年前

    set<Class A, a function pointer>

    class typename