小编b.g*_*.g.的帖子

使用模板重载函数

我正在尝试使用模板定义一个函数,并且我希望 typename 为 int 或 anEnum(我定义的特定枚举)。我尝试了以下方法,但失败了:

template <int | anEnum T> // or <int T, anEnum T> or <int, anEnum T>
bool isFunction(const T &aVariable){}
Run Code Online (Sandbox Code Playgroud)

我想做的是使用模板,而不是定义两个重载函数。我更喜欢按如下方式调用函数,而程序员不必考虑类型

isFunction(aVariable) // and not isFunction<int> (aVariable) nor isFunction<anEnum> (aVariable)
Run Code Online (Sandbox Code Playgroud)

基本上,我希望这个函数被模板化为 int 和 aNum 类型。我已经搜索过这个,但找不到答案。我可能缺少什么?谢谢,

c++ templates overloading

33
推荐指数
2
解决办法
1686
查看次数

带有默认构造函数的 unordered_map emplace

我的 unordered_map 分别带有 string 和 aClass 类型的键值对;aClass 不能移动的地方(它有一个互斥锁)。我也不希望它是复制构造的;我认为复制构造一个包含互斥锁的类是不明智的。为了将项目的构建延迟到其插入到地图中,我尝试使用 emplace 将其插入到地图中,因此,第二个参数必须为空:

aList.emplace("aString");
Run Code Online (Sandbox Code Playgroud)

但是,前一行不起作用。任何想法如何使用默认构造函数放置?我也试过:

aList.emplace("aString", void);
aList.emplace("aString", {});
aList.emplace(std::piecewise_construct,"aString");
Run Code Online (Sandbox Code Playgroud)

谢谢,

c++ unordered-map emplace c++14

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

标签 统计

c++ ×2

c++14 ×1

emplace ×1

overloading ×1

templates ×1

unordered-map ×1