树莓派异常 no template named 'remove_cv_t' in namespace 'std'; did you mean 'remove_cv'?

平台: 树莓派4b
因由: npm 运行前端项目的时候,编译 node-sass ,出现了该异常

c++ 版本

1
2
3
4
5
6
7
8
9
$ c++ -v
Using built-in specs.
COLLECT_GCC=c++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/aarch64-linux-gnu/10/lto-wrapper
Target: aarch64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 10.2.1-6' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-10 --program-prefix=aarch64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libquadmath --disable-libquadmath-support --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --enable-fix-cortex-a53-843419 --disable-werror --enable-checking=release --build=aarch64-linux-gnu --host=aarch64-linux-gnu --target=aarch64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-mutex
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.1 20210110 (Debian 10.2.1-6)

npm 版本

1
2
3
4
5
$ node -v
v18.3.0

$ npm -v
8.11.0

异常代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
In file included from /root/.node-gyp/18.3.0/include/node/v8-local-handle.h:12,
from /root/.node-gyp/18.3.0/include/node/v8-array-buffer.h:12,
from /root/.node-gyp/18.3.0/include/node/v8.h:24,
from /root/.node-gyp/18.3.0/include/node/node.h:73,
from ../node_modules/nan/nan.h:56,
from ../src/binding.cpp:1:
/root/.node-gyp/18.3.0/include/node/v8-internal.h: In function ‘void v8::internal::PerformCastCheck(T*)’:
/root/.node-gyp/18.3.0/include/node/v8-internal.h:646:38: error: ‘remove_cv_t’ is not a member of ‘std’; did you mean ‘remove_cv’?
646 | !std::is_same<Data, std::remove_cv_t<T>>::value>::Perform(data);
| ^~~~~~~~~~~
| remove_cv
/root/.node-gyp/18.3.0/include/node/v8-internal.h:646:38: error: ‘remove_cv_t’ is not a member of ‘std’; did you mean ‘remove_cv’?
646 | !std::is_same<Data, std::remove_cv_t<T>>::value>::Perform(data);
| ^~~~~~~~~~~
| remove_cv
/root/.node-gyp/18.3.0/include/node/v8-internal.h:646:50: error: template argument 2 is invalid
646 | !std::is_same<Data, std::remove_cv_t<T>>::value>::Perform(data);
| ^
/root/.node-gyp/18.3.0/include/node/v8-internal.h:646:63: error: ‘::Perform’ has not been declared
646 | !std::is_same<Data, std::remove_cv_t<T>>::value>::Perform(data);
| ^~~~~~~

解决方案

打开 /root/.node-gyp/18.3.0/include/node/v8-internal.h,找到以下的代码:

1
2
3
4
5
template <class T>
V8_INLINE void PerformCastCheck(T* data) {
CastCheck<std::is_base_of<Data, T>::value &&
!std::is_same<Data, std::remove_cv<T>>::value>::Perform(data);
}

remove_cv_t 改成 remove_cv ,再次回到项目用 npm 运行即可。

分析

remove_cv_t 是在 c++ 14 之后提供支持…

据 github 上有人测试可以在 mac 上选择调用 CXXFLAGS="--std=c++14" npm i 成功编译 node-sass,我的派没得 c++14,也不想装,所以无效。

相关的 github issues

https://github.com/nodejs/node/issues/38367