C++ 可变模板,谁是对的?GCC还是叮当声?
我有以下代码:C++ 可变模板,谁是对的?GCC还是叮当声?,c++,c++11,g++,variadic-templates,clang++,C++,C++11,G++,Variadic Templates,Clang++,我有以下代码: enum class type { zero, one, two }; std::ostream& operator<<(std::ostream &os, type const &t) { switch(t) { case type::zero: os << "zero"; break;
enum class type
{
zero,
one,
two
};
std::ostream& operator<<(std::ostream &os, type const &t)
{
switch(t)
{
case type::zero: os << "zero"; break;
case type::one: os << "one"; break;
case type::two: os << "two"; break;
default: os.setstate(std::ios_base::failbit);
}
return os;
}
template <typename K>
using pv = std::pair<type, std::vector<K>>;
template <typename K>
using ps = std::pair<type, K>;
template <typename B, typename F, typename K>
void rec(int j, F &f, ps<K> const &s0) // 4
{
auto [n, v] = s0;
f(j, n, v);/*pro*/
std::cout << std::endl;
}
template <typename B, typename F, typename K, typename... T>
void rec(int j, F &f, ps<K> const &s0, T const &... t) // 3
{
auto [n, v] = s0;
f(j, n, v);/*pro*/
rec<B>(j, f, t...);
}
template <typename B, typename F, typename K, typename... T>
void rec(int j, F &f, pv<K> const &t0, ps<K> const &s0, T const &... t) // 2
{
auto [n, v] = t0;
for(auto k : v) {
rec<B>(j, f, s0, t..., ps<K>{n, k});
}
}
template <typename B, typename F, typename K, typename... T>
void rec(int j, F &f, pv<K> const &t0, T const &... t) // 1
{
auto [n, v] = t0;
for(auto k : v) {
rec<B>(j, f, t..., ps<K>{n, k});
}
}
void pro(int j, type t, std::string const &s)
{
std::cout << j << ":" << t << ":" << s << ", ";
}
int main()
{
std::vector<std::string> v0{"V00", "V01"};
std::vector<std::string> v1{"V10", "V11"};
std::vector<std::string> v2{"V20", "V21"};
int j = 0;
rec<char>(j, pro, pv<std::string>{type::zero, v0}, pv<std::string>{type::one, v1}, pv<std::string>{type::two, v2});
return 0;
}
为什么第二个代码与g++一起工作而不是第一个?类型的定义在哪里?@cigien添加了类型的定义。很抱歉错过了。这似乎是一个已在中修复的错误。您应该编辑该问题以使
类型
可打印。@cigien添加了打印类型
。再次抱歉!
main.cpp: In function ‘int main()’:
main.cpp: error: call of overloaded ‘rec<char>(int&, void (&)(int, type, const string&), std::pair<type, std::vector<std::__cxx11::basic_string<char> > >, std::pair<type, std::vector<std::__cxx11::basic_string<char> > >, std::pair<type, std::vector<std::__cxx11::basic_string<char> > >)’ is ambiguous
rec<char>(j, pro, pv<std::string>{type::zero, v0}, pv<std::string>{type::one, v1}, pv<std::string>{type::two, v2});
^
main.cpp: note: candidate: void rec(int, F&, ps<K>&, const T& ...) [with B = char; F = void(int, type, const std::__cxx11::basic_string<char>&); K = std::vector<std::__cxx11::basic_string<char> >; T = {std::pair<type, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::pair<type, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}; ps<K> = std::pair<type, std::vector<std::__cxx11::basic_string<char> > >]
void rec(int j, F &f, ps<K> const &s0, T const &... t) // 3
^~~
main.cpp: note: candidate: void rec(int, F&, pv<K>&, const T& ...) [with B = char; F = void(int, type, const std::__cxx11::basic_string<char>&); K = std::__cxx11::basic_string<char>; T = {std::pair<type, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::pair<type, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}; pv<K> = std::pair<type, std::vector<std::__cxx11::basic_string<char> > >]
void rec(int j, F &f, pv<K> const &t0, T const &... t) // 1
^~~
enum class type
{
zero,
one,
two
};
std::ostream& operator<<(std::ostream &os, type const &t)
{
switch(t)
{
case type::zero: os << "zero"; break;
case type::one: os << "one"; break;
case type::two: os << "two"; break;
default: os.setstate(std::ios_base::failbit);
}
return os;
}
template <typename K>
using pv = std::pair<type, std::vector<K>>;
template <typename K>
using ps = std::pair<type, K>;
template <typename F, typename K>
void rec(int j, F &f, ps<K> const &s0) // 4
{
auto [n, v] = s0;
f(j, n, v);/*pro*/
std::cout << std::endl;
}
template <typename F, typename K, typename... T>
void rec(int j, F &f, ps<K> const &s0, T const &... t) // 3
{
auto [n, v] = s0;
f(j, n, v);/*pro*/
rec(j, f, t...);
}
template <typename F, typename K, typename... T>
void rec(int j, F &f, pv<K> const &t0, ps<K> const &s0, T const &... t) // 2
{
auto [n, v] = t0;
for(auto k : v) {
rec(j, f, s0, t..., ps<K>{n, k});
}
}
template <typename F, typename K, typename... T>
void rec(int j, F &f, pv<K> const &t0, T const &... t) // 1
{
auto [n, v] = t0;
for(auto k : v) {
rec(j, f, t..., ps<K>{n, k});
}
}
void pro(int j, type t, std::string const &s)
{
std::cout << j << ":" << t << ":" << s << ", ";
}
int main()
{
std::vector<std::string> v0{"V00", "V01"};
std::vector<std::string> v1{"V10", "V11"};
std::vector<std::string> v2{"V20", "V21"};
int j = 0;
rec(j, pro, pv<std::string>{type::zero, v0}, pv<std::string>{type::one, v1}, pv<std::string>{type::two, v2});
return 0;
}