c++ - 使用定义相互依赖

标签 c++ templates visual-c++ using c++17

我有一些相互依赖的模板实例。通常我只是转发声明它们,但我不明白这是怎么可能的。这是一个例子

#include <tuple>
#include <memory>

using Tuple = std::tuple<int,TupleContainer>;
using TupleContainer = std::unique_ptr<Tuple>;

int main()
{
    return 0;
}

不能写Tuple首先是因为需要 TupleContainer , 不能写 TupleContainer首先是因为需要 Tuple .

如何转发声明其中一个 using 定义?

最佳答案

我设法通过在 std::tuple 周围使用薄包装类并使用前向声明来做到这一点。

#include <tuple>
#include <memory>

struct Tuple;
using TupleContainer = std::unique_ptr<Tuple>;

struct Tuple : public std::tuple<int,TupleContainer>{
    using std::tuple<int,TupleContainer>::tuple;
};

int main()
{
    return 0;
}

关于c++ - 使用定义相互依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48314612/

相关文章:

c++ - 使用 C++ AMP 时未命中 GPU 断点

c++ - 在 C++ 中将另一个类作为类成员

C++连续读取文件

c++ - 吃 bean 人运动不顺畅

c++ - 为什么不将模板类型参数推断为 'const' ?

c++ - 具有不同数量参数的函数的模板别名

c++ - 类方法的模板特化。 "Function template already defined"

c++ - 智能指针而不是指针

mysql - 使用 windows api 函数 CreateProcessA 初始化 mysql

C++11 std::thread vs windows CreateThread