【发布时间】:2018-11-27 08:05:00
【问题描述】:
我正在尝试使用 Boost.Mp11 检查一个特殊类型元组的 unspecialised 唯一性:
#include <iostream>
#include <vector>
#include <deque>
#include <tuple>
#include <boost/mp11/algorithm.hpp>
namespace
{
template <typename T, template <typename...> typename U>
struct is_specialisation : std::false_type {};
template <template <typename...> typename U, typename... Args>
struct is_specialisation<U<Args...>, U> : std::true_type {};
template <template <typename...> typename U>
struct is_specialisation_meta
{
template <typename T>
using type = is_specialisation<T, U>;
};
template <typename TypeList>
struct unique_specialisation
{
template <typename T>
using type = std::is_same<
boost::mp11::mp_count_if<
TypeList,
is_specialisation_meta<T>::template type // Error!
>,
boost::mp11::mp_size_t<1>
>;
};
}
int main()
{
using types = std::tuple<
std::vector<int>,
std::deque<int>,
std::tuple<int>
>;
using all_unique_specialisations = boost::mp11::mp_all_of<
types,
unique_specialisation<types>::template type
>;
std::cout << std::boolalpha << all_unique_specialisations::value << std::endl;
return EXIT_SUCCESS;
}
您可以在Coliru 上运行上述代码。对于每一种类型,整个列表都会被迭代,试图找到一个非专业化的等价物,所以{std::vector<int>, std::deque<float>, std::tuple<Foo>} 会通过,但{std::vector<int>, std::vector<float>, std::tuple<Foo>} 不会。
但是我得到了这个错误:
main.cpp:30:37: error: type/value mismatch at argument 1 in template parameter list for 'template<template<class ...> class U> struct {anonymous}::is_specialisation_meta'
is_specialisation_meta<T>::template type
^
main.cpp:30:37: note: expected a class template, got 'T'
但我不明白T 是如何未知的 - 谁能看到我做错了什么?
【问题讨论】:
-
究竟什么是“非专业等价物”?我不明白你的例子
{std::vector<int>, std::deque<float>, std::tuple<Foo>}vs{std::vector<int>, std::vector<float>, std::tuple<Foo>} -
@m.s.
std::vector<int>是int在std::vector上的特化,因此std::vector是 unspecialised 类型。该算法试图找到多个非专业类型(即它检查非专业的唯一性)。第一个示例通过,因为每个非专业类型都不同,而第二个示例失败,因为有两个std::vectors。
标签: c++ boost boost-mp11