sitmo prng、c++11 和犰狳的编译问题

Compilation issue with sitmo prng, c++11, and armadillo

我正在尝试编译使用 sitmo 的 sitmo prng under C++11 within an R package. The problematic code has been packaged and is available here. The objective of this R package is to make available the sitmo header file so that other packages are able to use the LinkTo field within description. As an added bonus, the package is scheduled to ship with an Armadillo + OpenMP example. There is one other package, mvnfast,但仅限于 c++98 和 boost headers。

我认为我收到的错误特定于 OS Xclang。我无法通过 win-build 在 Windows 上复制它。话虽如此,错误是:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/random:3641:44: error: non-type template argument is not a constant expression
        const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;

错误仅在 Rcpp dev list 上弹出。这种情况下的解决方案是在 C++98 下编译并使用 boost。

以上错误后面有如下注释:

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/random:3773:18: note: in instantiation of function template specialization 'std::__1::generate_canonical<double, 53, sitmo::prng_engine>' requested here
        * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/random:3737:17: note: in instantiation of function template specialization 'std::__1::uniform_real_distribution<double>::operator()<sitmo::prng_engine>' requested here
        {return (*this)(__g, __p_);}
                ^
sitmo_test.cpp:77:26: note: in instantiation of function template specialization 'std::__1::uniform_real_distribution<double>::operator()<sitmo::prng_engine>' requested here
      double u = distunif(engine);
                         ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/random:3641:44: note: non-constexpr function 'max' cannot be used in a constant expression
    const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
                                           ^
../inst/include/prng_engine.hpp:100:23: note: declared here
  static result_type (max)() { return 0xFFFFFFFF; }

正在使用的 clang 版本是:

Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix

查看代码,sitmo 中存在错误 prng_engine.h。 min()max() 被声明为

static result_type (min)() { return 0; }

static result_type (max)() { return 0xFFFFFFFF; }

如果您查看 here 的标准 LCG max,您会发现它被声明为 constexprmin 也是如此。

一旦您在 sitmo 头文件中创建这些方法 constexpr,我相信您可以在模板表达式中使用它们。

更新

我查看了 GCC 5 头文件,确实声明了方法 constexpr