I've got a quick sample:
#include <utility>
using namespace std;
struct A{
int i;
char c;
};
void f(const A&){}
template<class T>
void g(T&& t)
{
f(forward<T>(t));
}
int main() {
A a={1,'@'};//OK
f({1,'#'});//OK
g({1,'@'});//Compilation error
return 0;
}
Clang will give this error:
testArray.cpp:16:5: error: no matching function for call to 'g'
g({1,'@'});//fix: g<A>({1,'@'})
^
testArray.cpp:9:6: note: candidate template ignored: couldn't infer
template argument 'T'
void g(T&& t)
^
My questions are:
In
A a={1,'@'};, if{}is deduced asstd::initializer_list, then how is it converted fromstd::initilizer_listto typeA?In
f({1,'#'});, whenfrequires a typeA, does the compiler implicitly generate anAobject, or does it convert fromstd::initializer_listtoA?why, when
g()is a template, does the template type deduction not work to give a typeA? Doesstd::forwardhelp to convey the message fromftog, say thatTis typeA?