阿尔卑斯构建环境中的 C++ 模板

c++ templates in alpine build environments

本文关键字:C++ 模板 构建 环境 阿尔卑斯      更新时间:2023-10-16

我不知为什么这样的非类型模板参数构造

template <typename TValue, typename TFile, size_t PAGESIZE>
inline typename Size<Buffer<TValue, PageFrame<TFile, Fixed<PAGESIZE> > > >::Type
capacity(Buffer<TValue, PageFrame<TFile, Fixed<PAGESIZE> > > const &)
{
  return PAGESIZE;
}

会用Alpines buildbase/gcc/stdlibc++/cmake软件包绊倒clang(4.0.0(和g++(6.3.0(。这发生在阿尔卑斯山:

file_page.h:76:22: error: expected ',' or '>' in template-parameter-list
    template <size_t PAGESIZE>
                     ^
/usr/include/limits.h:44:18: note: expanded from macro 'PAGESIZE'
#define PAGESIZE PAGE_SIZE
                 ^
/usr/include/bits/limits.h:3:19: note: expanded from macro 'PAGE_SIZE'
#define PAGE_SIZE 4096
                  ^

在我看来,宏观扩张在这里非常有意。任何解释都值得赞赏

精简后,您的代码类似于以下内容:

#include <cstddef>
#define PAGE_SIZE 4096
#define PAGESIZE PAGE_SIZE
template<std::size_t PAGESIZE>
void f() {}

您正在使用与扩展到特定值的宏相同的名称来命名非类型模板参数。就好像你写了:

#include <cstddef>
template<std::size_t 4096>
void f() {}

这显然是无效的语法。

如果要将非类型模板参数的默认值设置为宏的值,可以这样编写:

#include <cstddef>
#define PAGE_SIZE 4096
#define PAGESIZE PAGE_SIZE
template<std::size_t page_size = PAGESIZE>
void f() {}

但是,请确保在函数中使用page_size而不是PAGESIZE宏。