小编InF*_*s X的帖子

什么是C ++ 17中的constexpr容器?

我想创建一个类,其方法类似于std::map,但应在编译时进行排序。哪些constexpr容器适合存储键template<class K>和值template<class V>

std :: vector不满足这些要求。

UPD:我们发现std::array有很多constexpr方法。这足以解决我的问题std::array<std::pair<K, V> >。但是问题仍然存在。

c++ templates constexpr

4
推荐指数
1
解决办法
1723
查看次数

使用std :: unique_ptr <T>而不是std :: unique_ptr <T>有什么好处吗?

使用std::unique_ptr<T>&而不是std::unique_ptr<T>?例如,在函数参数中?

c++ unique-ptr

4
推荐指数
2
解决办法
486
查看次数

如何创建可以为T选择特殊容器的模板类?

如何创建一个模板类,可以理解类型T是否可以清洗,如果它是std::unodered_set用于收集类型为T?的元素.否则,我希望它使用std::set.

该类具有与上述相同的简单方法,sets例如insert(), find()等等.

template<class T>
class MySet {
public:
    MySet() {}
    bool find() {
    //some code here
Run Code Online (Sandbox Code Playgroud)

我检查类型是否operator()必须获取有关其"可靠性"的信息.为此,我使用以下构造,我在本网站上找到:

template<typename T>
class HasHash {
    typedef char one;
    typedef long two;

    template<typename C>
    static one test(decltype(&C::operator()));

    template<typename C>
    static two test(...);

public:
    enum {
        value = sizeof(test<T>(0)) == sizeof(size_t)
    };
};
Run Code Online (Sandbox Code Playgroud)

通过输入以下内容获取是否可以删除(真或假):

HasHash<HashableType>::value
Run Code Online (Sandbox Code Playgroud)

我没办法使用boost库.

c++ templates sfinae

3
推荐指数
1
解决办法
61
查看次数

指向接口的指针不是接口错误?

以下代码有效:

type Brace interface {}

type Round struct {
    prev_ Brace
}

type Square struct {}

func main() {
    var r Round
    var s Square
    r.prev_ = s
}
Run Code Online (Sandbox Code Playgroud)

这是真的,r.prev_现在是副本s吗?如何更改Round将包含指向Brace的指针?此代码不起作用:

type Brace interface {}

type Round struct {
    prev_ *Brace
}

type Square struct {}

func main() {
    var r Round
    var s Square
    r.prev_ = &s
}
Run Code Online (Sandbox Code Playgroud)

因为错误:

不能使用&s(类型*Square)作为类型*Brace赋值:*Brace是指向接口的指针,而不是接口

inheritance pointers go

0
推荐指数
1
解决办法
4256
查看次数

标签 统计

c++ ×3

templates ×2

constexpr ×1

go ×1

inheritance ×1

pointers ×1

sfinae ×1

unique-ptr ×1