I came up with this after answering this question
I had a simple function template (C ++ 11):
template<class elem_t, class list_t>
bool in_list(const elem_t& elem, const list_t& list) {
for (const auto& i : list) {
if (elem == i) {
return true;
}
}
return false;
}
But GCC issued warnings because it does not look like the output of a template parameter in the form of std :: initializer_list. Therefore, without hesitation, I made a specialization:
template<class elem_t>
bool in_list(const elem_t& elem, std::initializer_list<elem_t> list) {
for (const auto& i : list) {
if (elem == i) {
return true;
}
}
return false;
}
It worked. No more warnings. But when I looked again and thought about it, I remembered that C ++ does not support partial specialized specialization in function templates. But this is what it seems. My only assumption: this is allowed because std :: initializer_list is still dependent on the template parameter, so this is essentially a different template. But I'm not sure that this is how it should be (are there templates that do not overload?).
, ? ?
, GCC std:: initializer_list? , std:: initializer_list.
:
test.cpp: In function ‘int main()’:
test.cpp:33:43: warning: deducing ‘const list_t’ as ‘const std::initializer_list<int>’ [enabled by default]
test.cpp:6:6: warning: in call to ‘bool in_list(const elem_t&, const list_t&) [with elem_t = int, list_t = std::initializer_list<int>]’ [enabled by default]
test.cpp:33:43: warning: (you can disable this with -fno-deduce-init-list) [enabled by default]
in_list(3, {1, 2, 3, 4, 5});
: -, initializer_list GCC (cite). , : ++ 11? , , . !
EDIT2: , , GCC 4.7, , , , .