I have this code
#include <unistd.h>
#include <vector>
#include <iostream>
using namespace std;
std::string join(std::vector<std::variant<std::string, std::string_view>> input) {
return "";
}
int main(int argc, char* argv[]) {
string a = "hello";
string_view b = "world";
cout<<join(std::vector({a,b}))<<endl; // this returns error
vector<std::variant<string, string_view>> v;
v.push_back(a);
v.push_back(b);
cout<<join(v)<<endl; // this works
return 0;
}
I get the following error
candidate template ignored: deduced conflicting types for parameter '_Tp' ('string' (aka 'basic_string<char>') vs. 'string_view' (aka 'basic_string_view<char>'))
vector(initializer_list<value_type> __il);
My understanding is that when I am doing std::vector({a,b}), its assuming the vector will have same types for all arguments which is understandable.
How to make it work with that std::vector({a,b})? How can I change the join() function or its just not possible because the failure itself is at construction of vector?
std::variantjust because you initialize thevectorwith 2 different types is a bit too much.string atovariant<string, string_vew> a. Changestring_view btovariant<string, string_vew> b. The the compiler should be able to deduce the type of vector asvariant<string, string_vew>.