- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
给定
template <typename...> struct Pack;
using T1 = std::tuple<int, char, double>;
using T2 = std::tuple<bool, double, int, char>;
TupleTree<Pack, T1, T2>
应该是
Pack<
Pack<int, bool>, Pack<int, double>, Pack<int, int>, Pack<int, char>,
Pack<char, bool>, Pack<char, double>, Pack<char, int>, Pack<char, char>,
Pack<double, bool>, Pack<double, double>, Pack<double, int>, Pack<double, char>
>
这扩展到任意数量的元组。定义足够容易理解吗?好的,我的程序运行正常:
但是现在我想把定义扩展到
`TupleTreeWithRepeats<P, std::index_sequence<Is...>, Tuples...>`
哪里Is...
将指示每个元组被重复使用的次数,直到移动到下一个元组。注意 <Is...> = <1,1,...,1>
将减少到与 TupleTree<P, Tuples...>
相同.我遇到的歧义在于这两个专业:
TupleTreeWithRepeatsHelper<P, std::index_sequence<Is...>, LoopNumber, P<Ts...>, First, Rest...>
TupleTreeWithRepeatsHelper<P, std::index_sequence<I, Is...>, I, P<Ts...>, First, Rest...>
由于某种原因,P<Ts...>
的存在导致歧义,因为当我用单命名类型替换它时,歧义就被消除了。即使我更换 std::index_sequence<Is...>
与 std::index_sequence<I, Is...>
,歧义仍然存在。这是怎么回事?以及如何解决这个问题?这是代码,与 TupleTree
几乎相同:
#include <iostream>
#include <tuple>
#include <type_traits>
template <typename T> struct Identity { using type = T; };
// Merging packs of types.
template <typename...> struct MergePacks;
template <typename Pack>
struct MergePacks<Pack> : Identity<Pack> {};
template <template <typename...> class P, typename... Types1, typename... Types2, typename... Packs>
struct MergePacks<P<Types1...>, P<Types2...>, Packs...> : MergePacks<P<Types1..., Types2...>, Packs...> {};
// Appending a type to a pack.
template <typename Pack, typename T> struct AppendType;
template <template <typename...> class P, typename... Ts, typename T>
struct AppendType <P<Ts...>, T> {
using type = P<Ts..., T>;
};
// ExpandPackWithTuple takes a pack, and creates N packs that each end with the tuple's elements, N is the size of the tuple.
template <template <typename...> class P, typename Pack, typename Tuple, typename Indices> struct ExpandPackWithTupleHelper;
template <template <typename...> class P, typename Pack, typename Tuple, std::size_t... Is>
struct ExpandPackWithTupleHelper<P, Pack, Tuple, std::index_sequence<Is...>> {
using type = P<typename AppendType<Pack, typename std::tuple_element<Is, Tuple>::type>::type...>;
};
template <template <typename...> class P, typename Pack, typename Tuple>
using ExpandPackWithTuple = typename ExpandPackWithTupleHelper<P, Pack, Tuple, std::make_index_sequence<std::tuple_size<Tuple>::value>>::type;
// TupleTreeWithRepeats.
template <template <typename...> class P, typename NumRepeats, std::size_t LoopNumber, typename OutputPack, typename... Tuples> struct TupleTreeWithRepeatsHelper;
template <template <typename...> class P, std::size_t I, std::size_t... Is, std::size_t LoopNumber, typename... Ts, typename First, typename... Rest>
struct TupleTreeWithRepeatsHelper<P, std::index_sequence<I, Is...>, LoopNumber, P<Ts...>, First, Rest...> :
TupleTreeWithRepeatsHelper<P, std::index_sequence<I, Is...>, LoopNumber + 1, typename MergePacks<ExpandPackWithTuple<P, Ts, First>...>::type, First, Rest...> {};
template <template <typename...> class P, std::size_t I, std::size_t... Is, typename... Ts, typename First, typename... Rest>
struct TupleTreeWithRepeatsHelper<P, std::index_sequence<I, Is...>, I, P<Ts...>, First, Rest...> :
TupleTreeWithRepeatsHelper<P, std::index_sequence<Is...>, 0, typename MergePacks<ExpandPackWithTuple<P, Ts, First>...>::type, Rest...> {};
template <template <typename...> class P, std::size_t... Is, std::size_t LoopNumber, typename OutputPack>
struct TupleTreeWithRepeatsHelper<P, std::index_sequence<Is...>, LoopNumber, OutputPack> {
using type = OutputPack;
};
template <template <typename...> class P, typename NumRepeats, typename... Tuples> struct TupleTreeWithRepeats;
template <template <typename...> class P, std::size_t I, std::size_t... Is, typename... Tuples>
struct TupleTreeWithRepeats<P, std::index_sequence<I, Is...>, Tuples...> : TupleTreeWithRepeatsHelper<P, std::index_sequence<Is...>, 0, P<P<>>, Tuples...> {};
// Testing
template <typename...> struct Pack;
using T1 = std::tuple<int, char, double>;
using T2 = std::tuple<bool, double, int, char>;
using T3 = std::tuple<double, int>;
int main() {
std::cout << std::is_same<
TupleTreeWithRepeats<Pack, std::index_sequence<1,1,1>, T1, T2, T3>::type,
Pack<
Pack<int, bool, double>, Pack<int, bool, int>, Pack<int, double, double>, Pack<int, double, int>, Pack<int, int, double>, Pack<int, int, int>, Pack<int, char, double>, Pack<int, char, int>,
Pack<char, bool, double>, Pack<char, bool, int>, Pack<char, double, double>, Pack<char, double, int>, Pack<char, int, double>, Pack<char, int, int>, Pack<char, char, double>, Pack<char, char, int>,
Pack<double, bool, double>, Pack<double, bool, int>, Pack<double, double, double>, Pack<double, double, int>, Pack<double, int, double>, Pack<double, int, int>, Pack<double, char, double>, Pack<double, char, int>
>
>::value << '\n'; // ambiguous
}
最佳答案
据我所知,这两个应该没有歧义。
无论如何,一个简单的解决方法是根据“无重复”来实现“有重复”:
// TupleTreeWithRepeats.
template <template <typename...> class P, typename Tuples>
struct TupleTreeWithRepeatsHelper;
template <template <typename...> class P, typename... Tuples>
struct TupleTreeWithRepeatsHelper<P, Pack<Tuples...>> :
Identity<TupleTree<P, Tuples...>> {};
template <template <typename...> class P, typename NumRepeats, typename... Tuples>
struct TupleTreeWithRepeats;
template <template <typename...> class P, std::size_t... Is, typename... Tuples>
struct TupleTreeWithRepeats<P, std::index_sequence<Is...>, Tuples...> :
TupleTreeWithRepeatsHelper<P, typename MergePacks<repeat<Tuples, Is>...>::type> {};
repeat
在哪里
template<class T, std::size_t> using id = T;
template<class T, std::size_t... Is>
Pack<id<T, Is>...> do_repeat(std::index_sequence<Is...>);
template<class T, std::size_t I>
using repeat = decltype(do_repeat<T>(std::make_index_sequence<I>()));
Demo .有人可能会争辩说这也是一种更好的设计。
最小化为:
template<class> class Purr { };
template <template <class> class, class>
struct Meow;
template <template <class> class P>
struct Meow<P, P<int>> { };
template <template <class> class P, class T>
struct Meow<P, P<T>> { };
Meow<Purr, Purr<int>> c;
GCC 报告了一个歧义,这对我来说绝对像是一个错误。 Clang 正确地处理了这个问题。
关于c++ - 无法解释不明确的模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32916242/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
正如您在 this travis.yml 中看到的那样文件,我的代码依赖于一些第三方库,我在构建项目之前将它们安装在远程系统上。 Travis 每次推送提交时都会下载并构建这些库,这可以避免吗?我的意
我是一名优秀的程序员,十分优秀!