【发布时间】:2018-12-03 09:40:21
【问题描述】:
我有一些使用模板的复杂第三方代码。使用 gcc 7.3.0,如果指定了 gcc 标志 -std=gnu++98,则它可以构建,但否则会给出编译错误(即使用 C++11 编译)。我需要修复 C++11 编译。代码如下(抱歉不完整,头文件比较复杂):
#define CPP11 (__cplusplus > 199711L)
namespace csl
{
namespace PostExec
{
struct Complement
{
static int64_t Execute(int64_t v)
{
return ~v;
}
};
template<size_t bitWidth, bool maskOutput>
struct Mask
{
static int64_t Execute(int64_t v)
{
return 0;
}
};
#if CPP11
template<typename P, typename... PS>
struct Compound
{
template<typename T>
static T&& Execute(T&& v)
{
if (sizeof...(PS) > 0)
{
return Compound<PS...>::Execute(P::Execute(std::forward<T>(v))); <<<< COMPILER ERROR HERE
}
return P::Execute(std::forward<T>(v));
}
};
#else
template<typename P1, typename P2>
struct Compound
{
static int64_t Execute(int64_t v)
{
return P1::Execute(P2::Execute(v));
}
};
#endif
}
}
using namespace csl;
class CModel
{
public:
void f1();
private:
void Execute() { }
static const size_t PAGE_COUNT = 1;
static size_t CurrentPage;
static CPage<1, 0> State[PAGE_COUNT];
};
void CModel::f1()
{
int64_t n[5];
StepLogicalNXOr<1, false>::Execute(Page.N[1], n[0], n[1]);
}
size_t CModel::CurrentPage = 0;
CPage <1919, 0> CModel::State[PAGE_COUNT] = {
CPage <1, 0>()
};
编译器错误(定义 CPP11 时)为:
<snip>: error: wrong number of template arguments (0, should be at least 1)
return Compound<PS...>::Execute(P::Execute(std::forward<T>(v)));
~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<snip>: note: provided for ‘template<class P, class ... PS> struct csl::PostExec::Compound’
struct Compound
^~~~~~~~
我知道这很复杂,但如果有人能提供帮助,我将不胜感激。
【问题讨论】:
标签: c++ c++11 templates variadic-templates template-meta-programming