如果我使用gcc编译以下代码
namespace TMP {
// template to choose type depending on boolean condition
template <bool condition, typename x, typename y> struct if_t { typedef y type; };
template < typename x, typename y> struct if_t<true, typename x, typename y> { typedef x type; };
}
TMP::if_t<false, uint8_t, uint16_t>::type test;
我收到错误消息
error: wrong number of template arguments (2, should be 3)
如果删除第二个模板,它将成功编译。但是我认为我的代码与书wikibook example中的代码几乎相同。我的错是什么?
答案 0 :(得分:1)
实际上,只需删除冗余的typename。用gcc 4.9.2编译。
namespace TMP {
// template to choose type depending on boolean condition
template <bool condition, typename x, typename y>
struct if_t
{
typedef y type;
};
template <typename x, typename y>
struct if_t < true, x, y >
{
typedef x type;
};
}
答案 1 :(得分:0)
在yufeng的帮助下,我发现了它应该如何完成:
namespace TMP {
// template to choose type depending on boolean condition
template <bool condition, typename x, typename y>
struct if_t { typedef y type; };
template <typename x, typename y>
struct if_t<true, typename x, typename y> { typedef x type; };
}
TMP::if_t<false, uint8_t, uint16_t>::type test;