【发布时间】:2020-10-22 01:37:44
【问题描述】:
我有一个 c++17 库,可以在我的 Mac 上编译,但不能在我大学的集群 (Linux) 上编译。
我的编译器是:
Apple clang 版本 11.0.0 (clang-1100.0.33.17)
集群编译器是:
g++ (Spack GCC) 9.2.0
我的一位同事也安装了该库,并在其机器(总是 Mac)上成功编译。
我试图将所有内容都简化为主要(我猜)错误,将所有内容都放在下面的代码中。
这是错误:
error: invalid use of incomplete type 'class TensorBase<double, std::integer_sequence<long unsigned int, 0> >'
116 | class Matrix<Real,1,1,NonZeroRow_>: public TensorBase<Real, std::make_index_sequence<1>>
下面我放了代码。我基本上有一个BaseTensor 类,Matrix 类继承自该类。在这样的班级中,我也有专长。
#include <cstdlib>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <numeric>
#include <array>
using Integer=long;
using Real=double;
template <typename T, std::size_t>
using getTypeSequence = T;
template <typename, typename>
class TensorBase;
template <typename T, Integer ... Is>
class TensorBase<T, std::index_sequence<Is...>>
{
protected:
std::array<T, sizeof...(Is)> values{};
static const std::size_t Size = sizeof...(Is);
public:
constexpr TensorBase (getTypeSequence<T, Is> ... vals)
: values{{vals...}}
{}
constexpr TensorBase (std::array<T, sizeof...(Is)> const & a)
: values{a}
{}
constexpr TensorBase (std::array<T, sizeof...(Is)> && a)
: values{std::move(a)}
{}
// TensorBase(std::initializer_list<T> a)
// {
// assert(a.size() == Size);
// std::copy(std::begin(a), std::end(a), std::begin(values));
// }
constexpr TensorBase () = default;
~TensorBase() = default;
constexpr TensorBase (TensorBase const &) = default;
constexpr TensorBase (TensorBase &&) = default;
constexpr TensorBase & operator= (TensorBase const &) = default;
constexpr TensorBase & operator= (TensorBase &&) = default;
};
template<typename T, Integer Rows_, Integer Cols_,Integer NonZeroRow_=-1>
class Matrix: public TensorBase<T, std::make_index_sequence<Rows_*Cols_>>
{
public:
static constexpr Integer Rows=Rows_;
static constexpr Integer Cols=Cols_;
using type= Matrix<T,Rows,Cols>;
using subtype=T;
using MB = TensorBase<T, std::make_index_sequence<Rows*Cols>>;
using MB::MB;
using MB::values;
static constexpr Integer NonZeroRow=NonZeroRow_;
inline constexpr static Integer rows() { return Rows; }
inline constexpr static Integer cols() { return Cols; }
inline constexpr std::array<T,Rows*Cols> &operator()()
{
return values;
}
inline constexpr const std::array<T,Rows*Cols> &operator()()const
{
return values;
}
// access matrix direclty by using I*Col+J index
inline constexpr T &operator()(const Integer i)
{
assert(i < Rows*Cols);
return values[i];
}
inline constexpr const T &operator()(const Integer i)const
{
assert(i < Rows*Cols);
return values[i];
}
inline constexpr T &operator()(const Integer i, const Integer j)
{
assert(i < Rows);
assert(j < Cols);
return values[i*cols() + j];
}
inline constexpr const T &operator()(const Integer i, const Integer j) const
{
assert(i < Rows);
assert(j < Cols);
return values[i*cols() + j];
}
};
template<Integer NonZeroRow_>
class Matrix<Real,1,1,NonZeroRow_>: public TensorBase<Real, std::make_index_sequence<1>>
{
public:
static constexpr Integer Rows=1;
static constexpr Integer Cols=1;
using T= Real;
using type= Matrix<T,Rows,Cols>;
using subtype=T;
using MB = TensorBase<T, std::make_index_sequence<Rows*Cols>>;
using MB::MB;
using MB::values;
static constexpr Integer NonZeroRow=NonZeroRow_;
inline constexpr static Integer rows() { return Rows; }
inline constexpr static Integer cols() { return Cols; }
inline constexpr std::array<T,Rows*Cols> &operator()()
{
return values;
}
inline constexpr const std::array<T,Rows*Cols> &operator()()const
{
return values;
}
inline constexpr T &operator[](const Integer i)
{
assert(i < Rows);
return values[i];
}
inline constexpr T &operator[](const Integer i)const
{
assert(i < Rows);
return values[i];
}
// access matrix direclty by using I*Col+J index
inline constexpr T &operator()(const Integer i)
{
assert(i < Rows*Cols);
return values[i];
}
inline constexpr const T &operator()(const Integer i)const
{
assert(i < Rows*Cols);
return values[i];
}
inline constexpr T &operator()(const Integer i, const Integer j)
{
assert(i < Rows);
assert(j < Cols);
return values[i*cols() + j];
}
inline constexpr const T &operator()(const Integer i, const Integer j) const
{
assert(i < Rows);
assert(j < Cols);
return values[i*cols() + j];
}
};
int main(int argc, char *argv[])
{
constexpr Matrix<Real,1,1> mat{1.0};
std::cout<<"it works"<<std::endl;
static_assert(mat(0,0)==1.0);
return 0;
}
【问题讨论】:
-
我在你的代码中没有看到明确的#include
定义了所有相关的序列类型。可能一个编译器隐式包含这个,第二个没有。 -
感谢您的回复。我也尝试将其包含在内,但它也不起作用。
-
我认为不完整的类型错误是因为选择了 TensorBase 的主模板而不是专业化。我做了一个较小的例子,显示 gcc 在类似的场景中选择主模板godbolt.org/z/4YZkCU 我认为 gcc 选择主模板是错误的,但我不是 100% 确定。
标签: c++ compiler-errors c++17 cluster-computing compile-time