我已经在Haskell编程语言中使用了Scrap Your Boilerplate和Uniplate库,我会发现通过有区别的联合的泛型编程形式非常有用.f#编程语言中是否有等效的库?
FUNC(param);
Run Code Online (Sandbox Code Playgroud)
如果param是char *,派遣func_string.
什么时候int发送到func_int
我认为可能有一个解决方案,因为变量类型在编译时是已知的.
也在programmers.stackexchange.com上:
我知道STL概念必须存在,并且将它们称为"类"或"接口"是愚蠢的,而实际上它们只是文档化(人类)概念,当时无法转换为C++代码,但是当有机会扩展语言来容纳概念时,他们为什么不简单地修改类和/或引入接口的功能?
是不是一个非常类似于接口的概念(100%抽象类没有数据)?通过观察它,在我看来接口只是缺乏对公理的支持,但也许公理可以引入C++的接口(考虑假设采用C++中的接口来接管概念),不是吗?我认为甚至可以轻松地将自动概念添加到这样的C++接口(自动界面LessThanComparable,任何人?).
Concept_map 与Adapter模式非常相似吗?如果所有方法都是内联的,那么适配器在编译时间之外基本上不存在; 编译器只是用内联版本替换对接口的调用,在运行时直接调用目标对象.
我听说过一种称为静态面向对象编程的东西,它实质上意味着在泛型编程中有效地重用面向对象的概念,从而允许使用大多数OOP的功能而不会产生执行开销.为什么不进一步考虑这个想法?
我希望这很清楚.如果你认为我不是,我可以改写这个; 请告诉我
我想将CRTP模式与一些锁定机制结合使用,以便在多线程环境中进行访问同步.
我的代码看起来像这样:
//-- CRTP base class with some sync/lock mechanism
template<typename T, typename SYNC>
struct Base {
static std::unordered_map<int, std::string> s_map;
static SYNC s_sync;
};
//-- derived class using CRTP
template<typename SYNC>
struct ProductX : public Base<ProductX<SYNC>, SYNC> {};
//-- static initialisation
template<typename SYNC>
std::unordered_map<int, std::string> Base<ProductX<SYNC>, SYNC>::s_map {
{ 1, "value_1" },
{ 2, "value_2" }
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了
错误:非模板的模板定义
std::unordered_map<int, std::basic_string<char> > Base<ProductX<SYNC>, SYNC>::s_map
编译时
静态s_map初始化引发错误.有人能指出我做错了什么吗?
假设您有一个模板参数T.
有什么区别
add_cv_t<T> 和 const volatile Tadd_const_t<T> 和 const Tadd_volatile_t<T> 和 volatile Tadd_lvalue_reference_t<T> 和 T&add_rvalue_reference_t<T> 和 T&&add_pointer_t<T>和T*?我为什么要用add_rvalue_reference_t<T>而不是T&&举个例子.有什么规则可供选择吗?
我目前正在处理一些我没写过的Haskell代码,但是我已经对它进行了修改.我的更改后,我运行该程序并收到以下错误消息:
Prelude.!!: index too large
Run Code Online (Sandbox Code Playgroud)
调用!!不在我的代码中,因此重构它比我想要的工作更多,如果我可以避免它.
我想要的是做这样的事情:
class PrintList a where
(!!) :: [a] -> Int -> a
instance (Show a) => PrintList a where
l (!!) n = if n < (length l)
then (l Prelude.!! n)
else error ("Index " ++ show n ++ " out of bounds in " ++ show l )
instance PrintList a where
(!!) = Prelude.!!
Run Code Online (Sandbox Code Playgroud)
即为!!每个可能的列表类型定义函数,但只要为元素类型定义了Show实例,它的行为就不同了.
或者,一种tryShow :: a -> Maybe String方法也可以做到这一点.
有没有办法做到这一点?只有在Show实现不适用时,才能强制OverlappingInstances使用默认实现吗?这是保证的行为吗?
编辑:任何可以获得错误的人也可以打印类似堆栈跟踪的消息!
haskell generic-programming typeclass ghc overlapping-instances
在Idris中,有一些神奇的机器可以自动为用户定义的类型创建(依赖)消除器.我想知道是否可以使用Haskell类型做一些事情(可能更少依赖).例如,给定
data Foo a = No | Yes a | Perhaps (Foo a)
Run Code Online (Sandbox Code Playgroud)
我想生成
foo :: b -> (a -> b) -> (b -> b) -> Foo a -> b
foo b _ _ No = b
foo _ f _ (Yes a) = f a
foo b f g (Perhaps c) = g (foo b f g x)
Run Code Online (Sandbox Code Playgroud)
我对多变量函数和泛型非常弱,所以我可以使用一些帮助入门.
我似乎无法通过搜索找到答案,所以这里......
我知道我可以通过利用这种类型的代码将Class对象一般传递给其他类:
public class ClsGeneric<TObject> where TObject : class
{
public TObject GenericType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后以这种方式构建:
ClsGeneric<MyType> someName = new ClsGeneric<MyType>()
Run Code Online (Sandbox Code Playgroud)
但是,我有一个应用程序,要求我打开一个表单,并以某种方式传递泛型类型以便在该表单中使用.我试图能够为许多不同的类类型重用此表单.
有谁知道这是否可能,如果可能,如何?
我已经使用Form构造函数进行了一些实验,但无济于事.
非常感谢,戴夫
更新:澄清我想要达到的结果是什么
更新:今年4月4日,我进一步前进,但我提供了解决方案的赏金.这就是我现在拥有的:
interface IFormInterface
{
DialogResult ShowDialog();
}
public class FormInterface<TObject> : SubForm, IFormInterface where TObject : class
{ }
public partial class Form1 : Form
{
private FormController<Parent> _formController;
public Form1()
{
InitializeComponent();
_formController = new FormController<Parent>(this.btnShowSubForm, new DataController<Parent>(new MeContext()));
}
}
public class FormController<TObject> where TObject : class
{
private …Run Code Online (Sandbox Code Playgroud) 所以我试图创建一个类型特征,说明两个“外部”类类型是否相同。
IE。std::vector<int>与 相同std::vector<double>,我不关心我的类型特征的任何内部参数。
我曾与试图弥补这方面的通用型特质的问题是,我只知道如何处理的类型可变参数模板分别对非类型化的,这似乎从使它通用阻止我。
是否可以处理类型化和非类型化模板参数的任何排列?
这是我实施的内容(包括失败的示例):
// g++ -std=c++17
#include <iostream>
#include <vector>
#include <array>
#include <type_traits>
// If the outer types don't match
template <typename, typename>
struct is_outer_type_same : std::false_type {};
// if the arguments of the compared Type contains only types
// ie. std::vector<int,std::allocator<int>>
// (the inner arguments are also types)
template <template<typename...> typename OuterType,
typename... InnerTypes1,
typename... InnerTypes2
>
struct is_outer_type_same < OuterType<InnerTypes1...>,
OuterType<InnerTypes2...>
>
: std::true_type …Run Code Online (Sandbox Code Playgroud) c++ generic-programming type-traits template-meta-programming variadic-templates