我有一个通用的React组件,像这样说:
class Foo<T> extends React.Component<FooProps<T>, FooState> {
constructor(props: FooProps<T>) {
super(props);
render() {
return <p> The result is {SomeGenericFunction<T>()}</p>;
}
}
Run Code Online (Sandbox Code Playgroud)
我也有一个看起来与此类似的HOC(但没有意义):
export const withTd =
<T extends WithTdProps>(TableElement: React.ComponentType<T>): React.SFC<T> =>
(props: T) => <td><TableElement {...props}/></td>;
Run Code Online (Sandbox Code Playgroud)
但是当我使用这样的组件时:
const FooWithTd = withTd(Foo);
Run Code Online (Sandbox Code Playgroud)
没有方法可以传递类型参数,因为您既不能withTd(Foo<T>)也不可以做FooWithTd,则类型总是错误的。这样做的正确方法是什么?
编辑:问题是我希望<FooWithTd<number> {...someprops}/>以后能有类似的东西,因为我T在HOC中不知道所需的类型。
给定一个可变参数模板参数包,我想检查使用inline constexpr bool和折叠表达式给出的所有类型是否唯一.我喜欢这样的东西:
template<class... T>
inline static constexpr bool is_unique = (... && (!is_one_of<T, ...>));
Run Code Online (Sandbox Code Playgroud)
is_one_of类似的bool 在哪里正常工作.但是无论我将什么放入is_one_of,这一行都无法编译.甚至可以使用折叠表达式来完成,还是我需要为此目的使用常规结构?
我需要证明:256 * (x / 256) <= 256 * x / 256,或更一般地说forall a b c : N, c > 0 -> a * (b / c) <= a * b / c。这是真的,因为要么 b 可以被 c 整除并且它们相等,要么不相等,首先乘法可以扩大数字并导致更高的精度。但是,我在标准库中找不到任何可以证明这一点的定理,而且我知道的任何自动策略(auto、intuition、easy、zify 和 omega)都不起作用。如果有帮助,我也知道x < 256 * 256,但是检查所有 65536 个案例并不是一个很好的证明......
我创建了一个派生自std :: exception的自定义异常类.
#include <iostream>
class Exception : std::exception {
public:
const char* what() const noexcept override {
return "test";
}
};
int main() {
try {
throw Exception();
} catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
这个程序在Ubuntu上由g ++ -stdc ++ = 17编译时,导致异常不会被catch块捕获,即使按引用捕获也应该捕获派生的异常.它调用std :: terminate,即使它发生在try块中,它通过引用捕获它的基类.如果Exception继承自std :: runtime_error并将"test"传递给自己的构造函数中的std :: runtime_error构造函数,则会发生同样的事情.通常解决方案只是使用Exception捕获,但在我的原始代码中,我需要捕获不同类型的异常,所有异常都继承自std :: exception.为什么会这样?参考基地的捕捉不起作用吗?如何使用一个catch块捕获从std :: exception派生的所有异常?
c++ ×2
c++17 ×1
coq ×1
exception ×1
generics ×1
polymorphism ×1
proof ×1
reactjs ×1
templates ×1
typescript ×1