考虑下面的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相应的基类的构造函数.
谢谢,
#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) #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) 最近我注意到模板特化的以下问题.
请注意,如果我们具有以下特化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) 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 …