我有涉及可变参数模板参数的部分特化问题。带前缀的专业化
template<typename A, typename ... B>
struct Foo<A, B...> { };
按预期工作,但当我尝试与postfix匹配时
template<typename A, typename ... B>
struct Foo<B..., A> { };
它不知何故不起作用。是否有一些我不知道的规则或编译器问题? (我正在使用G ++ 7.4,特别是来自cygwin的x86_64-w64-mingw32-g ++)
自包含示例来演示我的问题:
#include <iostream>
template<char ... C>
struct Str
{
static constexpr char Value[] = { C..., '\0' };
};
template<char ... C>
constexpr char Str<C...>::Value[];
template<typename>
struct TrimFront;
template<char A, char ... C>
struct TrimFront<Str<A, C...>>
{
typedef Str<C...> Type;
};
template<typename>
struct TrimBack;
template<char A, char ... C>
struct TrimBack<Str<C..., A>>
{
typedef Str<C...> Type;
};
int main(int, char **)
{
typedef Str<'a', 'b', 'c', 'd', 'e', 'f'> str_t;
std::cout << str_t::Value << std::endl; // prints "abcdef"
std::cout << TrimFront<str_t>::Type::Value << std::endl; // prints "bcdef"
std::cout << TrimBack<str_t>::Type::Value << std::endl; // ERROR (incomplete type)
return 0;
}
我想
template<char A, char ... C>
struct TrimBack<Str<C..., A>>
{
typedef Str<C...> Type;
};
不能推断(“A
”和“C...
”)不能推断,因为可变背包(C...
)不在最后位置。
OP合理地要求参考
真?非常不幸。你可以偶然指出它在标准中所说的位置吗?我似乎找不到相关的部分
我不是语言层,但在我看来相关部分(C ++ 11标准)是14.8.2.5(“从类型中推断模板参数”,“[temp.deduct.type]”),第9点(强调)矿)
如果
P
具有包含<T>
或<i>
的形式,则将相应模板参数列表P_i
的每个参数P
与A_i
的相应模板参数列表的相应参数A
进行比较。如果P
的模板参数列表包含的包扩展不是最后一个模板参数,则整个模板参数列表是非推导的上下文。如果P_i
是包扩展,则将P_i
的模式与A
的模板参数列表中的每个剩余参数进行比较。每个比较都推导出P_i
扩展的模板参数包中后续位置的模板参数。
所以,如果我没错,TrimBack<str_t>
(a.k.a。TrimBack<Str<'a', 'b', 'c', 'd', 'e', 'f'>>
)会给出错误,因为
1)在第一阶段,Str<C..., A>
匹配Str<'a', 'b', 'c', 'd', 'e', 'f'>
2)但在第二阶段,试图推断C...
和A
类型,P
(在这个阶段是Str<C..., A>
)“包含一个不是最后一个模板参数的包扩展”,所以“整个模板参数列表是非 - 受到限制的背景“。
像这样的部分类模板专门化
template<typename> struct TrimBack;
template<char ...C, char A> struct TrimBack<Str<C..., A>> {}
是不允许的,因为要推断C...
和A
,执行deduction from a type,并且不是最后一个包装参数使它成为non-deduced context。
你可以做的是使用帮助器类型“打开”包,然后“重新包装”它,减去最后一个元素。
template <char ...P>
struct dummy {};
template <class T, char ...P>
struct internal;
template <char ...P1, char T, char ...P2>
struct internal<dummy<P1...>, T, P2...>
{
using type = typename internal<dummy<P1..., T>, P2...>::type; // unwrap one recursively
};
template <char ...P1, char T>
struct internal<dummy<P1...>, T>
{
using type = Str<P1...>; // re-wrap all but the last one
};
template <typename>
struct TrimBack;
template <char ...C>
struct TrimBack<Str<C...>>
{
using Type = typename internal<dummy<>, C...>::type;
};
现在这应该工作:
std::cout << TrimBack<str_t>::Type::Value << std::endl; // prints "abcde"
这是使用boost::mp11
的解决方案:
评论内联:
#include <iostream>
#include <boost/mp11.hpp>
template<char ... C>
struct Str
{
static constexpr char Value[] = { C..., '\0' };
};
template<char ... C>
constexpr char Str<C...>::Value[];
template<typename>
struct TrimFront;
template<char A, char ... C>
struct TrimFront<Str<A, C...>>
{
typedef Str<C...> Type;
};
template<typename>
struct TrimBack;
using namespace boost::mp11;
// a means of turning chars into types
template<char c> struct c_char {
constexpr char value() { return c; }
};
// a means of turning an mp_list of c_char<char>... back into a Str<char...>
template<typename>
struct back_to_Str;
template<char...cs>
struct back_to_Str<mp_list<c_char<cs>...>>
{
using result = Str<cs...>;
};
// TrimBack using types as computation steps:
template<char... C>
struct TrimBack<Str<C...>>
{
// turn the input chars into an mp_list of c_char
// always use types, they're much easier than values when metaprogramming
using input = mp_list<c_char<C>...>;
// reverse the list
using reversed = mp_reverse<input>;
// pop the front c_char<>
using popped = mp_pop_front<reversed>;
// reverse again
using re_reversed = mp_reverse<popped>;
// turn back into a Str<char...>
using Type = typename back_to_Str<re_reversed>::result;
};
int main(int, char **)
{
typedef Str<'a', 'b', 'c', 'd', 'e', 'f'> str_t;
std::cout << str_t::Value << std::endl; // prints "abcdef"
std::cout << TrimFront<str_t>::Type::Value << std::endl; // prints "bcdef"
std::cout << TrimBack<str_t>::Type::Value << std::endl; // prints "abcde"
return 0;
}
预期产量:
abcdef
bcdef
abcde
http://coliru.stacked-crooked.com/a/387e5dc7ef262f1f
凭借我们新发现的知识,我们可以简化:
#include <iostream>
#include <boost/mp11.hpp>
using namespace boost::mp11;
template<char c>
struct c_char {
static constexpr char value() { return c; }
};
template<typename...> struct Str;
template<char... C>
struct Str<c_char<C>...>
{
static constexpr auto size() -> std::size_t { return sizeof...(C) + 1; }
static constexpr char Value [size()] = { C..., '\0' };
};
template<char...C> using make_Str = Str<c_char<C>...>;
template<typename List>
struct TrimFront
{
using Type = mp_pop_front<List>;
};
template<typename List>
struct TrimBack
{
using Type = mp_reverse<mp_pop_front<mp_reverse<List>>>;
};
int main(int, char **)
{
using str_t = make_Str<'a', 'b', 'c', 'd', 'e', 'f'>;
std::cout << str_t::Value << std::endl; // prints "abcdef"
std::cout << TrimFront<str_t>::Type::Value << std::endl; // prints "bcdef"
std::cout << TrimBack<str_t>::Type::Value << std::endl; // prints "abcde"
return 0;
}