小编Bri*_*uez的帖子

在Python中经过双重检查的锁定线程安全吗?

双重检查锁定习语是不是在某些语言可靠,我想知道的Python是否是其中之一。更具体地说,是以下代码...

# Objects shared by threads:
obj = None
lock_for_obj = threading.Lock()

def get_obj():
    """Function called concurrently by threads."""
    if obj is None:
        with lock_for_obj:
            if obj is None:
                obj = factory()  # Never returns `None`
    return obj
Run Code Online (Sandbox Code Playgroud)

... Python中的线程安全?有没有的场景/实现?为什么?

python multithreading language-lawyer

13
推荐指数
1
解决办法
405
查看次数

使用sfinae的std :: hash专业化?

作为练习,我试图看看是否可以使用SFINAE创建std::hash专门化,std::pair并且std::tuple当其所有模板参数都是无符号类型时.我对它们有一点经验,但据我所知,哈希函数需要已经模板化,typename Enabled = void我可以添加一个特化.我不确定从哪里开始.这是一种无效的尝试.

#include <functional>
#include <type_traits>
#include <unordered_set>
#include <utility>

namespace std {
template <typename T, typename Enabled = void>
struct hash<std::pair<T, T>, std::enable_if_t<std::is_unsigned<T>::value>>
{
    size_t operator()(const std::pair<T, T>& x) const
    {
        return x;
    }
};
}; // namespace std


int
main(int argc, char ** argv)
{
    std::unordered_set<std::pair<unsigned, unsigned>> test{};
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误:

hash_sfinae.cpp:7:42: error: default template argument in a class template partial specialization
template <typename T, typename Enabled …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae template-specialization c++14

8
推荐指数
1
解决办法
813
查看次数

元组是不是按顺序构建的?

以下程序:

#include <iostream>
#include <tuple>

struct A {
    A() { std::cout << "A constructor\n"; }
};

struct B {
    B() { std::cout << "B constructor\n"; }
};

int main() {
    std::tuple<A, B> t;
}
Run Code Online (Sandbox Code Playgroud)

在不同的编译器上提供不同的输出:

# libstdc++
B constructor
A constructor
# libc++
A constructor
B constructor
Run Code Online (Sandbox Code Playgroud)

这看起来很奇怪......我认为标准会保证元组元素按顺序构造,例如A,B,...,Y,Z?

c++ constructor stdtuple

7
推荐指数
1
解决办法
264
查看次数

如何将 static_cast&lt;T&gt; 作为函数传递?

我有一个类型A,设计为隐式转换为 type B。这是我想使用它的示例:

// Current implementation:
std::transform(vec_of_a.begin(), vec_of_a.end(), std::back_inserter(vec_of_b),
               [](const A& a) -> B { return a; });  // Thanks, Kerrek SB.

// Ideal implementation - Won't compile, expected '(' after 'static_cast'.
std::transform(vec_of_a.begin(), vec_of_a.end(), std::back_inserter(vec_of_b),
               static_cast<B>);
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能使后一个选项编译?

functor static-cast c++11

6
推荐指数
1
解决办法
1380
查看次数

为什么`boost :: hana :: range_c`不是序列?

#include <string>
#include <utility>
#include <vector>
#include <boost/hana.hpp>
namespace hana = boost::hana;

template <typename ...T>
void indexed_T_work(T&& ...args)
{
    auto indices = hana::range_c<std::size_t, 0, sizeof...(T)>;
    auto types = hana::make_tuple(std::forward<T>(args)...);
    hana::for_each(
        hana::zip(indices, types)
      , [](auto&& pair_) { /* Do index-dependent work with each `T` */ }
        );
}

int main()
{
    indexed_T_work(5, 13, std::vector<std::string>{}, 32.f, 42, "foo");
}
Run Code Online (Sandbox Code Playgroud)

我想用hana::zipa hana::tuplehana::range_c,但hana::range_c不被认为是一个序列,这是一个要求hana::zip.这个决定背后的原因是什么?我如何(惯用)在尊重这个决定的同时实现我的目标?

c++ boost-hana

5
推荐指数
1
解决办法
241
查看次数

将两个序列拉入地图中

static constexpr auto type_tuple_c = hana::tuple_t<T...>;
static constexpr auto idx_tuple_c = hana::tuple_c<std::size_t, 0, sizeof...(T)>;
Run Code Online (Sandbox Code Playgroud)

我想将这两个大小相等的序列相互映射.但是,我似乎无法理解如何使用这些hana::map功能:

static constexpr auto type_idx_map_c = hana::unpack(
    hana::zip_with(hana::make_pair, type_tuple_c, idx_tuple_c)
  , hana::make_map
);
Run Code Online (Sandbox Code Playgroud)

无论我做了什么转换,我似乎都无法创建映射.我知道地图要求其元素属于产品概念,但我似乎无法获得(甚至理解)与压缩结构相关的行为.

有什么我可以做的,或者我做错了什么?

跑步gcc version 6.0.0 20160320hana version 0.7.0最后一次取得今天

c++ boost boost-hana

5
推荐指数
1
解决办法
163
查看次数

在 RAII 构造中修改 RVO 值是否安全?

考虑以下程序:

#include <functional>
#include <iostream>

class RvoObj {
  public:
    RvoObj(int x) : x_{x} {}
    RvoObj(const RvoObj& obj) : x_{obj.x_} { std::cout << "copied\n"; }
    RvoObj(RvoObj&& obj) : x_{obj.x_} { std::cout << "moved\n"; }

    int x() const { return x_; }
    void set_x(int x) { x_ = x; }

  private:
    int x_;
};

class Finally {
  public:
    Finally(std::function<void()> f) : f_{f} {}
    ~Finally() { f_(); }

  private:
    std::function<void()> f_;
};

RvoObj BuildRvoObj() {
    RvoObj obj{3};
    Finally run{[&obj]() { obj.set_x(5); }};
    return …
Run Code Online (Sandbox Code Playgroud)

c++ destructor raii copy-elision rvo

5
推荐指数
1
解决办法
126
查看次数

从函数的参数构建模板?

template<class... Foos> // N = sizeof...(Foos)
template<typename... Args> // M = sizeof...(Args)
void split_and_call(Args&&... args)
{
    // Using Python notation here...
    Foos[0](*args[:a]);  // a = arity of Foos[0]
    Foos[1](*args[a:b]); // b-a = arity of Foos[1]
    ...
    Foos[N-1](*args[z:M]); // M-z = arity of Foos[N-1]
}
Run Code Online (Sandbox Code Playgroud)

假设:

  • 所有类型Foos都可以调用
  • 所有类型Foos都是明确的
  • 任何类型都Foos可能具有0的arity
  • Args 是所用的所有参数类型的串联 Foos

这可以在公开Foos 公平的情况下完成Args吗?即使我明确地列出了它们,我实际上也不确定该怎么做.

c++ template-meta-programming variadic-templates c++14

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

我在哪里可以找到Python内置函数引发的异常?

我正在写一个装饰器来验证一些功能.我尝试尽可能地使用内置函数来完成繁重的工作,但是我一直在坚持选择使用它时应该捕获的异常.

例如:

def Validated(fun):
    def ValidatedFun(*args, **kwargs):
        try:
            _ = dict(kwargs.get('untrusted_data', ()))
        except ? as e:
            raise BetterError('Additional relevant info') from e
        return fun(*args, **kwargs)
    return ValidatedFun
Run Code Online (Sandbox Code Playgroud)

我想知道:

  • dict(以及其他内置插件)明确提出的最常见的异常是什么?
  • 我在哪里可以找到列出它们的文档?(它们不在 https://docs.python.org/上)

python exception-handling exception

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

STL 和 Hana 元组之间的转换

#include <boost/hana.hpp>
#include <iostream>
#include <tuple>

namespace hana = boost::hana;

int main()
{
    int x{7};
    float y{3.14};
    double z{2.7183};
    auto t = hana::to<hana::tuple_tag>(std::tie(x, y, z));
    hana::for_each(t, [](auto& o) { std::cout << o << '\n'; });
}
Run Code Online (Sandbox Code Playgroud)

实现这一目标的 hana 方法是什么?我意识到我可以使用: hana::make_tuple(std::ref(x), std::ref(y), std::ref(z)),但这似乎不必要地冗长。

c++ boost-hana

2
推荐指数
1
解决办法
645
查看次数

tmux绑定快捷方式在左上方或左侧创建窗格?

无论何时使用tmux split-window -h/v,它都会分别创建右侧/底部的新拆分.我想要一个在一边创建新分割的命令(即左侧/顶部),但我无法在任何地方找到任何简单的答案......如何将此行为绑定到快捷方式?

默认情况会发生什么:

 _____                            _______
|     |   == split-window -h ==> |   |   |
| *1  |                          |*1 | 2 |
 -----                            -------
Run Code Online (Sandbox Code Playgroud)

我想要一个快捷方式:

 _____                            _______
|     |   ==       ???       ==> |   |   |
| *1  |                          | 2 |*1 |
 -----                            -------
Run Code Online (Sandbox Code Playgroud)

tmux

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