小编Lin*_*Lin的帖子

python多继承使用super将构造函数传递给参数

考虑下面的python代码片段

class A(object):
    def __init__(self, a):
        self.a = a

class B(A):
    def __init__(self, a, b):
        super(B, self).__init__(a)
        self.b = b

class C(A):
    def __init__(self, a, c):
        super(C, self).__init__(a)
        self.c = c

class D(B, C):
    def __init__(self, a, b, c, d):
        #super(D,self).__init__(a, b, c) ???
        self.d = d
Run Code Online (Sandbox Code Playgroud)

我想知道如何传递a,b以及c相应的基类的构造函数.

谢谢,

python multiple-inheritance super diamond-problem

41
推荐指数
3
解决办法
2万
查看次数

c ++闭包创建

#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
  // Block 1
  {
    auto inc = []() { int i = 0; return [&]() { return i++; }; }();
    std::vector<int> v(10, 10);
    std::generate(v.begin(), v.end(), inc);
    for (auto i : v) std::cout << i << std::endl;
  }

  // Block 2
  {
    auto inc = []() { int i = 0; return [&]() { return i++; }; };
    std::vector<int> v(10, 10);
    std::generate(v.begin(), v.end(), inc());
    for (auto i : v) std::cout << i …
Run Code Online (Sandbox Code Playgroud)

c++ lambda closures c++11 c++14

9
推荐指数
1
解决办法
711
查看次数

c ++为什么std :: async比顺序执行慢

#include <future>
#include <iostream>
#include <vector>
#include <cstdint>
#include <algorithm>
#include <random>
#include <chrono>
#include <utility>
#include <type_traits>

template <class Clock = std::chrono::high_resolution_clock, class Task>
double timing(Task&& t, typename std::result_of<Task()>::type* r = nullptr)
{
  using namespace std::chrono;
  auto begin = Clock::now();
  if (r != nullptr) *r = std::forward<Task>(t)();
  auto end = Clock::now();
  return duration_cast<duration<double>>(end - begin).count();
}

template <typename Num>
double sum(const std::vector<Num>& v, const std::size_t l, const std::size_t h)
{
  double s;
  for (auto i = l; i <= …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading asynchronous c++11

9
推荐指数
1
解决办法
1351
查看次数

c ++模板专业化参数重复

最近我注意到模板特化的以下问题.

请注意,如果我们具有以下特化f,则模板参数名称可以是非常长的类型,可能来自其他模板.

template <class T> void f(T t) {}

template <>
void f<VeryLongType>(VeryLongType t)
{
    using T = VeryLongType;
    // ...
}
Run Code Online (Sandbox Code Playgroud)

请注意,这个非常长的类型名称重复3次.此外,如果f返回此类型的值,则将引入另一个重复(auto 将是一种解决方法).

我想知道是否存在一些简化的语法,以便不需要重复?

可能如下:

template <>
void f<T = VeryLongType>(T t)
{
   // ...
}
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

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

haskell - 连接函数的类型

data M a = M a deriving (Show)
unitM a = M a
bindM (M a) f = f a

joinM :: M (M a) -> M a
joinM m = m `bindM` id

joinM' :: M a -> a
joinM' m = m `bindM` id
Run Code Online (Sandbox Code Playgroud)

请注意,joinM (M 0)将无法键入检查,但会没问题joinM' (M 0).

我的问题:为什么joinM定义为M (M a) -> M a但不是M a -> a

从我的理解,
unitM把该值a放入单子中M a
joinM得到值a …

monads haskell

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