template<typename T> struct A
{
static T x(T, T) { }
static T y(T, T) { }
};
template<typename T> struct B
{
static T x(T, T) { }
static T y(T, T) { }
};
struct Dispatcher
{
template<template<typename> class TC, ??? TStaticFunc,
typename T1, typename T2>
static std::common_type_t<T1, T2> call(T1 mI, T2 mJ)
{
return TC<std::common_type_t<T1, T2>>::*TStaticFunc(mI, mJ);
}
};
Dispatcher::call<A, x>(12.f, 5);
Dispatcher::call<B, x>(1.f, 51);
Dispatcher::call<A, y>(2.f, 25);
Dispatcher::call<B, y>(5.f, 3);
Run Code Online (Sandbox Code Playgroud)
有可能创建一个像这样的模板化函数Dispatcher::call吗?我想分别传递类类型和静态函数名称.
或者是通过A<..:>::x实现这种派遣的唯一可行方式?
我试图从可变参数模板类型列表中获取最大的类型.我得到了意想不到的结果:
// Bigger between two types
template<typename T1, typename T2>
using Bigger = std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>;
// Recursion helper
template<typename...>
struct BiggestHelper;
// 2 or more types
template<typename T1, typename T2, typename... TArgs>
struct BiggestHelper<T1, T2, TArgs...>
{
using Type = Bigger<T1, BiggestHelper<T2, TArgs...>>;
};
// Exactly 2 types
template<typename T1, typename T2>
struct BiggestHelper<T1, T2>
{
using Type = Bigger<T1, T2>;
};
// Exactly one type
template<typename T>
struct BiggestHelper<T>
{
using Type = T;
};
template<typename... …Run Code Online (Sandbox Code Playgroud) 如何在一个std::unordered_map?中存储2个以上的变量?
我想要这样的东西:
std::unordered_map<string, int, int, int> mapss = {{"a",1,1,1},{"b",1,2,3}};
Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <type_traits>
using namespace std;
// Non-trivially-copyable type.
struct NTC
{
int x;
NTC(int mX) : x(mX) { }
~NTC() { cout << "boop." << x << endl; }
};
int main()
{
using AS = aligned_storage_t<sizeof(NTC), alignof(NTC)>;
// Create two `std::aligned_storage` instances
// and "fill" them with two "placement-new-constructed"
// `NTC` instances.
AS as1, as2;
new (&as1) NTC{2};
new (&as2) NTC{5};
// Swap the `aligned_storages`, not their contents.
std::swap(as1, as2);
// Explicitly call `~NTC()` on the …Run Code Online (Sandbox Code Playgroud) 忽略丢失的完美转发.(假设参数在实际实现中完美转发.)
// Base case: no args
template<typename TF> void forEach2Args(TF) { }
// Recursive case: some args
template<typename TF, typename... Ts> void forEach2Args(TF mFn, Ts... mXs)
{
mFn(getNth<0>(mXs...), getNth<1>(mXs...));
forEach2Args(mFn, getAllAfter<2>(mXs...));
}
int main()
{
int result{0};
forEach2Args([&result](auto a1, auto a2)
{
result += (a1 * a2);
}, 2, 4, 3, 6);
// roughly evaluates to:
// result += (2 * 4);
// result += (3 * 6);
}
Run Code Online (Sandbox Code Playgroud)
是否可以实现getNth并getAllAfter避免任何可能的运行时开销?到目前为止,我发现的唯一解决方案是在第一次调用时放入每个Ts...内部,然后将非const引用传递给每个递归调用.我几乎肯定有不必要的move/ctor/dtor电话.std::tupleforEach2Args
另一种解决方案是使用以下内容 …
考虑下面的最小示例,它提供了我在一些实际代码中遇到的情况:
use std::collections::HashSet;
type HS = HashSet<String>;
fn fn1(x: String, hs0: &mut HS, hs1: &mut HS) {
// ...
}
fn fn0(hs0: &mut HS, hs1: &mut HS) {
hs0.get("").map(|x| fn1(x.clone(), hs0, hs1));
}
fn main() {
let mut hs0 = HS::new();
let mut hs1 = HS::new();
fn0(&mut hs0, &mut hs1);
}
Run Code Online (Sandbox Code Playgroud)
借款检查员不满意:
error[E0500]: closure requires unique access to `hs0` but `*hs0` is already borrowed
--> <anon>:9:21
|
9 | hs0.get("").map(|x| fn1(x, hs0, hs1));
| --- ^^^ --- - borrow ends …Run Code Online (Sandbox Code Playgroud) 我有一个遗留的非阻塞dequeue API,如下所示:
template <typename T>
bool bad_queue<T>::try_dequeue(T* out);
// Returns 'false' if there was nothing to dequeue.
// Returns 'true' and sets '*out' to the dequeued item otherwise.
Run Code Online (Sandbox Code Playgroud)
我想包装bad_queue<T>到我自己的成员函数中cool_queue<T>返回一个:std::optional<T>try_dequeue
template <typename T>
class cool_queue
{
private:
bad_queue<T> _q;
public:
std::optional<T> try_dequeue();
};
Run Code Online (Sandbox Code Playgroud)
这是我目前实施的方式try_dequeue:
template <typename T>
std::optional<T> cool_queue<T>::try_dequeue()
{
T temp;
const auto ok = _q.try_dequeue(&temp);
if(!ok) { return std::nullopt; }
return {std::move(temp)};
}
Run Code Online (Sandbox Code Playgroud)
有没有办法避免临时temp缓冲区并直接构造_q.try_dequeue可选内部返回的值?例:
// PSEUDOCODE …Run Code Online (Sandbox Code Playgroud) 我的许多功能中都有以下模式:
use std::sync::{Arc, Mutex};
struct State {
value: i32
}
fn foo(data: Arc<Mutex<State>>) {
let state = &mut data.lock().expect("Could not lock mutex");
// mutate `state`
}
Run Code Online (Sandbox Code Playgroud)
&mut *data.lock().expect("Could not lock mutex") 一遍又一遍地重复,所以我想将它重构为一个函数,以便编写类似
let state = get_state(data);
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
fn get_state(data: &Arc<Mutex<State>>) -> &mut State {
&mut data.lock().expect("Could not lock mutex")
}
Run Code Online (Sandbox Code Playgroud)
哪个无法编译:
错误:无法返回引用临时值的值
这让我相信data.state.lock().expect("...")价值回报。但是,我可以看到状态通过在这个 playground 上的多次foo调用而发生变化。
这里发生了什么?为什么我看似简单的重构编译失败?
编辑:
我希望以下内容也能正常工作:
fn get_state<'a>(data: &'a Arc<Mutex<State>>) -> &'a mut State {
let state: &'a mut State = &mut …Run Code Online (Sandbox Code Playgroud) List<object> objects = new List<object>();
objects.Add(5);
Run Code Online (Sandbox Code Playgroud)
我想要做
objects[0] += 10;
Run Code Online (Sandbox Code Playgroud)
但我需要先把它抛出来.
int a = (int) objects[0];
a += 10;
Run Code Online (Sandbox Code Playgroud)
但这样做只会更改a,而不是列表中的整数.
解决这个问题的最佳方法是什么?
int main()
{
float* ptr;
{
float f{10.f};
ptr = &f;
}
*ptr = 13.f;
// Do more stuff with `*ptr`...
}
Run Code Online (Sandbox Code Playgroud)
它使用/访问有效或未定义的行为*ptr?
我测试了类似于上面示例的情况,并且所有内容似乎都起作用,好像嵌套块中变量的生命周期由于指针而被扩展.
我知道const&(const引用)将延长临时的生命周期.指针是否相同?