我想创建一个类,其方法类似于std::map
,但应在编译时进行排序。哪些constexpr
容器适合存储键template<class K>
和值template<class V>
?
std :: vector不满足这些要求。
UPD:我们发现std::array
有很多constexpr方法。这足以解决我的问题std::array<std::pair<K, V> >
。但是问题仍然存在。
使用std::unique_ptr<T>&
而不是std::unique_ptr<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库.
以下代码有效:
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是指向接口的指针,而不是接口