简体   繁体   English

明确选择应使用哪个模板重载

[英]Explicitly choose which template overload should be used

Can we chose which function template overload should be used in this case? 我们可以选择在这种情况下应该使用哪个函数模板重载?

struct X { };

struct A { A(X){} };
struct B { B(X){} };

template<class T>
void fun(T, A) { }

template<class T>
void fun(T, B) { }

int main() {
    /* explicitly choose overload */ fun(1, X());
}

Error: 错误:

error: call of overloaded 'fun(int, X)' is ambiguous
     /* explicitly choose overload */ fun(1, X());
                                                ^
    candidate: void fun(T, A) [with T = int]
 void fun(T, A) { }
      ^~~
    candidate: void fun(T, B) [with T = int]
 void fun(T, B) { }
      ^~~

For normal function it looks like this: 对于正常功能,它看起来像这样:

void fun(A){}
void fun(B){}

int main() {
    ((void(*)(A))(fun))(X());
}

Is it possible? 可能吗?

改善你的榜样,你可以尝试

 ((void(*)(int, A))(fun))(1, X());

If you choose not to explicitly specify the first parameter type but still want to specify the second you could go along with lambda dedicated for casting purpose (c++14 solution): 如果您选择不显式指定第一个参数类型但仍希望指定第二个参数类型,则可以使用专用于转换目的的lambda(c ++ 14解决方案):

struct X { };

struct A { A(X){} };
struct B { B(X){} };

template<class T>
void fun(T, A) { }

template<class T>
void fun(T, B) { }

int main() {
    [](auto v, auto x){ static_cast<void(*)(decltype(v), A)>(fun)(v, x); }(1, X());
}

[live demo] [现场演示]

A low-tech solution to this problem would be to add one extra level of indirection. 解决这个问题的一个低技术解决方案是添加一个额外的间接级别。 Add a function like funA , whose sole purpose is to give an explicit name to the first version of fun : 添加一个像funA这样的函数,其唯一目的是为第一个版本的fun提供一个明确的名称:

struct X { };

struct A { A(X){} };
struct B { B(X){} };

template<class T>
void fun(T, A) { }

template<class T>
void fun(T, B) { }

template <class T>
void funA(T t, A a) { fun(t, a); }

int main() {
    /* explicitly choose overload */ funA(1, X());
}

However, I wonder why you cannot just change the argument to A(X()) . 但是,我想知道为什么你不能只将参数改为A(X()) You will have to change the calling code anyway, so what's the problem? 无论如何你必须改变调用代码,那么问题是什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当参数不同时,重载分辨率不选择模板 - Overload resolution doesn't choose template when arguments are different 选择模板函数的重载还是函子(函数对象)的部分专业化 - Whether to choose an overload of template function or a partial specialization of a functor(function object) 可变参数模板函数的 1-arg 重载有用吗? 它应该使用魔法 &amp;&amp; 引用吗? 在这种情况下是否发生复制消除? - Is 1-arg overload of variadic template function useful? Should it used a magic && reference? Is Copy-Elision taking place in this case? 重载分辨率和数组:应该调用哪个函数? - Overload resolution and arrays: which function should be called? C ++如何选择要使用的运算符重载? - How does C++ choose which operator overload to use? 将显式指定的函数模板重载作为模板参数传递的正确语法是什么? - What's the correct syntax for passing an explicitly specified function template overload as a template parameter? 模板函数重载(泛型类型与模板模板类型)选择正确的重载 - Template function overloading (generic type vs template template type) choose correct overload 模板函数和此函数的非模板重载,哪个最匹配? - A template function and a non-template overload of this function, which is a best match? 如何在模板包扩展期间明确告诉编译器只选择一个参数模板? - How to explicitly tell compiler to choose exactly one parameter template during template pack expansion? 哪个转换应与模板类参数dynamic_cast或reinterpet_cast一起使用? - which cast should be used with a template class parameter, dynamic_cast or reinterpet_cast?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM