我的一些基类获得了大量的参数.现在我想指定要使用的静态函数:
template <typename... Types>
struct SBase {
static void func() {
}
};
struct A : public SBase<int> {
};
struct B : public A, public SBase<int, double, short,
unsigned int, float, unsigned char, long, unsigned long> {
// using SBase::func; // Not possible.
// Horrible, but works.
using SBase<int, double, short,
unsigned int, float, unsigned char, long, unsigned long>::func;
};
Run Code Online (Sandbox Code Playgroud)
您可以看到,我需要两次编写模板参数,这会导致代码重复.
有没有办法摆脱它?
template <typename T>
class rp {
};
template <template <typename> class P>
struct b {
template <class, template <typename> class FriendP>
friend void f(b<FriendP> from);
};
template <class, template <typename> class P>
void f(b<P> from) {
}
int main() {
b<rp> v;
f<int>(v);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Clang 3.3(svn)编译好,而GCC 4.8拒绝它:
main.cpp: In function 'int main()':
main.cpp:17:10: error: call of overloaded 'f(b<rp>&)' is ambiguous
f<int>(v);
^
main.cpp:17:10: note: candidates are:
main.cpp:12:6: note: void f(b<P>) [with <template-parameter-1-1> = int; P = rp]
void …Run Code Online (Sandbox Code Playgroud) trait Trait<T> {
fn equality() -> bool;
}
impl<T> PartialEq for Trait<T> {
fn eq(&self, other: &Trait<T>) -> bool {
self.equality()
}
}
Run Code Online (Sandbox Code Playgroud)
结果是
main.rs:5:23: 5:31 error: the trait `Trait` cannot be made into an object [E0372]
main.rs:5 impl<T> PartialEq for Trait<T> {
Run Code Online (Sandbox Code Playgroud)
删除静态方法使其可编译。带有 &self 参数的方法也可以编译。