Here I have series of overloaded functions that have as input either vector or initializer_list. And I want to handle the special case when client code inputs empty initializer_list. The problem is that compiler cannot determine what data was supposed in such empty list. So my question is how I address in function declaration such case.
#include <string>
#include <vector>
using namespace std;
void func(vector<string> v) { }
void func(vector<wstring> v) { }
void func(initializer_list<string> iv) {}
void func(initializer_list<wstring> iv) {}
int main() {
using namespace std;
func({"apple", "banana"});
func({L"蘋果", L"香蕉"});
func({}); // special case
}
error message:
<stdin>: In function 'int main()':
<stdin>:14:10: error: call of overloaded 'func(<brace-enclosed initializer list>)' is ambiguous
<stdin>:14:10: note: candidates are:
<stdin>:5:6: note: void func(std::vector<std::basic_string<char> >)
<stdin>:6:6: note: void func(std::vector<std::basic_string<wchar_t> >)
<stdin>:7:6: note: void func(std::initializer_list<std::basic_string<char> >)
<stdin>:8:6: note: void func(std::initializer_list<std::basic_string<wchar_t> >)
void func(initializer_list<void> iv) {}
- has no effect. I don't know how to properly declare it.
There's no way to distinguish this with the parameter alone. You can make one of them a template though, which will then be more costly and less preferable for overload resolution
void func(vector<string> v) { }
void func(vector<wstring> v) { }
template<typename = void>
void func(initializer_list<string> iv) {}
void func(initializer_list<wstring> iv) {}
Now calling func({})
will prefer the last function over the function template. Note that func({"hello", "world"})
still prefers the function template over the non-template vector-taking function, because the parameter conversion cost is more important than whether or not a candidate was synthesized from a template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With