我有一个接口块来定义一个通用子例程,该子例程有一个假定大小的数组作为虚拟参数(以便能够作用于传递数组的“中间”,如 C 指针),但它无法编译。这是一个简单的例子:
module foo
interface sub
module procedure isub
module procedure dsub
end interface
contains
subroutine isub(a,n)
integer, intent(in) :: a(*), n
integer :: i
print*, 'isub'
do i=1,n
print*, a(i)
enddo
end subroutine isub
subroutine dsub(a)
real(8), intent(in) :: a(*)
integer, intent(in) :: n
integer :: i
print*, 'dsub'
do i=1,n
print*, a(i)
enddo
end subroutine dsub
end module foo
program test
use foo
implicit none
integer :: ai(4)
real(8) :: ad(4)
ai=(/1,2,3,4/)
ad=(/1.,2.,3.,4./)
call sub(ai,3)
call sub(ad,3) …Run Code Online (Sandbox Code Playgroud) 我想实现一个可在任何容器或迭代器上调用的通用方法,该方法可以迭代特定类型,例如&[u32]、Vec<u32>、(0..99u32)等。
以下代码无法编译:
trait Foo { fn foo(self); }
impl Foo for std::iter::IntoIterator<Item=u32> {
fn foo(self) {}
}
Run Code Online (Sandbox Code Playgroud)
错误:必须指定关联类型的值
IntoIter(来自 Trait )[E0191]core::iter::IntoIteratorRun Code Online (Sandbox Code Playgroud)impl Foo for std::iter::IntoIterator<Item=u32> {
关联类型需要指定什么IntoIter?( std::iter::IntoIterator<Item=u32,IntoIter=???>)
这样就可以了:
vec![0u32].foo()
Run Code Online (Sandbox Code Playgroud) iterator traits generic-programming generic-collections rust
我需要使用函数/函数的返回值,而不知道它是什么类型(即作为模板).
虽然我可以将它传递给第二个函数而没有问题:
template <typename T>
void DoSomething(T value);
...
DoSomething(FunctionWhoseReturnedTypeIsUnknown(...));
Run Code Online (Sandbox Code Playgroud)
我想使用返回的内联值(无需调用第二个函数):
WhatGoesHere? x=FunctionWhoseReturnedTypeIsUnknown(...);
Run Code Online (Sandbox Code Playgroud)
这两种方法在概念上看起来与我相同(generic-programming-wize),但后者是否可以在C++中实现?
与我之前关于遍历数据结构的问题相关,当我将它与uniplate包一起使用时,我遇到了使代码变得通用的问题.我正在处理Language.Exts.Annotated.Syntax模块中的数据结构,它们都是类型参数的通用l.这l是整个树中的相同.
我写的代码是这样的:
doInt :: Child1 l -> Child1 l
doInt (Child1 l n) = Child1 l (n + 1)
doString :: Child2 l -> Child2 l
doString (Child2 l (_:s)) = Child2 l ('j' : s)
replace :: Data l => Parent l -> Parent l
replace = transformBi doInt
. transformBi doString
Run Code Online (Sandbox Code Playgroud)
此代码在最后两行产生以下错误:
Run Code Online (Sandbox Code Playgroud)Ambiguous type variable `l' in the constraint: `Data l' arising from a use of `transformBi' at Test.hs:31:10-52 Probable …
假设我template<int I> void ft()在结构模板中有一个静态函数模板template<bool B> S,我想ft从另一个函数模板调用template<bool B> void g(),将bool模板参数传递g给S:
template<bool B>
struct S {
static void f() {
}
template<int I>
static void ft() {
}
};
template<bool B>
void g() {
S<B>::f();
S<B>::ft<12>();
}
int main() {
g<true>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在GCC 4.5.2中进行编译会给出关于该行的两个错误S<B>::ft<12>():
Comeau(http://www.comeaucomputing.com/tryitout/),在严格的C++ 03模式下,也抱怨该行,说明"预期的表达式",在右括号下面有一个插入符号.S<B>::f()然而,编译器都没有抱怨该行,而且Comeau实际上可以在轻松模式下编译整个最小示例.
如果我删除了g模板,而是像这样实例化S模板参数g:
void …Run Code Online (Sandbox Code Playgroud) 为什么不能 void say(List< ? extends Number> list)被覆盖 void say(List< Number> list).
尝试编译时发生名称冲突.
这让我有一段时间的烦恼,但是有可能用VHDL描述类似于模板在C++中工作的实体(或者用于较小的扩展泛型吗?).只是在合成/编译过程中只保留实际的端口类型?
一个例子是多路复用器,比如我有一个4输入多路复用器,现在我有几个总线大小,我使用这个多路复用器,-4,6,7,8-.目前我为每种不同的总线大小写了一个不同的多路复用器; 但是输出只是转发的所选输入之一,因此与总线的类型相同.
这似乎过于冗余且容易出错(在正确的时间选择正确的多路复用器,使它们全部保持一致,在更改总线大小时更新它们).有没有办法参数化这个?
下面的非通用版本显示了这个想法.
entity mux_6bit_4input is
port ( input_0 : in std_logic_vector (5 downto 0);
input_1 : in std_logic_vector (5 downto 0);
input_2 : in std_logic_vector (5 downto 0);
input_3 : in std_logic_vector (5 downto 0);
sel : in std_logic_vector (1 downto 0);
output : out std_logic_vector (5 downto 0)
);
end entity mux_6bit_4input;
Run Code Online (Sandbox Code Playgroud) 我试图在Haxe3中为模板类型A编写一个通用比较函数(如c strcmp),假设此模板类型具有小于或等于运算符"<=".
我在Haxe3文档(http://haxe.org/manual/haxe3/features)中看到,如果您想假设模板类型具有新功能,您可以执行类似的工作:
@:generic static function foo<T:{function new(s:String):Void;}>(t:T) {
trace(Type.typeof(t)); // TClass([class String]) / TClass([class Template])
return new T("foo");
}
Run Code Online (Sandbox Code Playgroud)
所以,我用"le"函数尝试了相同的技术:
class Main {
@:generic static public function compare_<A:{function le(y:A):Bool;}>(x:A,y:A): Int {
if (x.le(y) && y.le(x)) return 0;
else if (x.le(y)) return -1;
else return 1;
}
static function main() {
var a:MyInt = new MyInt(1);
var b:MyInt = new MyInt(2);
trace(compare_(a,b));
}
}
class MyInt {
var data:Int;
public function new(i:Int) {this.data = i; }
public function …Run Code Online (Sandbox Code Playgroud) 如果我有一个简单的Utility函数将数组复制到一个新数组:
public static object[] CopyTo(object[] original, int startIndex, int endIndex)
{
List<object> copied - new List<object>();
for (int i = startIndex; i <= endIndex; i++)
{
copied.Add(original[i]);
}
return copied.ToArray();
}
Run Code Online (Sandbox Code Playgroud)
然后我希望能够像这样调用它:
int[] newThing = CopyTo(new int[] { 10, 9, 8, 7, 6 }, 2, 4);
Run Code Online (Sandbox Code Playgroud)
编译错误说cannot convert from int[] to object[].这是预期的,因为我的CopyTo函数特别想要一个对象数组,而不是一个整数数组.
如何更改CopyTo的声明以使其动态接受并返回任何类型的数组? 我相信泛型是这样的(尽管我对此并不太熟悉)所以我尝试过:
public static T[] CopyTo(T[] original, int startIndex......)
Run Code Online (Sandbox Code Playgroud)
但编译器不会将T识别为类型.
我试图>>用模板重载friend 操作符.我不想内联定义它.
我曾尝试add()在下面的代码中定义的方法的帮助下做同样的事情.它工作正常.我希望我的>>运营商也这样做.
以下是我的代码:
#include<iostream>
template<class T>class Demo;
template<class T>
std::ostream& operator<<(std::ostream&, const Demo<T> &);
template<class T>
std::istream& operator>>(std::istream&, const Demo<T> &);
template<class T>
class Demo {
private:
T data; // To store the value.
public:
Demo(); // Default Constructor.
void add(T element); // To add a new element to the object.
Demo<T> operator+(const Demo<T> foo);
friend std::ostream& operator<< <T>(std::ostream &out, const Demo<T> &d);
friend std::istream& operator>> <T>(std::istream &in, const Demo<T> &d);
};
template<class …Run Code Online (Sandbox Code Playgroud)