下面的代码片段在编译过程中会产生一个"foo的foo调用"错误,我想知道如果没有完全限定对foo的调用,是否有任何方法解决这个问题:
#include <iostream>
struct Base1{
void foo(int){
}
};
struct Base2{
void foo(float){
}
};
struct Derived : public Base1, public Base2{
};
int main(){
Derived d;
d.foo(5);
std::cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以,问题就像标题所说的那样.想法?我的意思是,以下工作完美无瑕:
#include <iostream>
struct Base{
void foo(int){
}
};
struct Derived : public Base{
void foo(float){
}
};
int main(){
Derived d;
d.foo(5);
std::cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在下面,struct Y重载了X成员函数f.两个重载都是模板函数,但是要明确指定不同的参数(typename和int):
struct X
{
template <typename> static bool f() { return true; }
};
struct Y : public X
{
using X::f;
template <int> static bool f() { return false; }
};
int main()
{
std::cout << Y::f <void>() << " " << Y::f <0>() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
1 0按预期使用gcc 打印.然而,clang(3.3)抱怨说
[...] error: no matching function for call to 'f'
std::cout << Y::f <void>() << " " << Y::f …Run Code Online (Sandbox Code Playgroud) c++ overloading member-functions using-declaration template-function
我有一个层次结构,代表HTTP客户端的某些部分,如下所示:
typedef list<pair<string, string> > KeyVal;
struct Header { string name; string value; ...};
struct Param { string name; string value; ...};
/* Something that contains headers */
template<typename T> class WithHeaders {
KeyVal headers;
public:
virtual T &operator <<(const Header &h) {
headers.push_back(pair<string, string>(h.name, h.value));
return static_cast<T&> (*this);
}
};
/* Something that contains query params */
template<class T> class WithQuery {
KeyVal query_params;
public:
virtual T &operator <<(const Param &q) {
query_params.push_back(pair<string, string>(q.name, q.value));
return static_cast<T&> (*this);
} …Run Code Online (Sandbox Code Playgroud)