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_list
to typeA
?In
f({1,'#'});
, whenf
requires a typeA
, does the compiler implicitly generate anA
object, or does it convert fromstd::initializer_list
toA
?why, when
g()
is a template, does the template type deduction not work to give a typeA
? Doesstd::forward
help to convey the message fromf
tog
, say thatT
is typeA
?