首页 > 解决方案 > clang++ 和 cl.exe 之间 C++ 模板代码的差异处理

问题描述

我得到了一段 c++ 代码,可以通过命令行“cl.exe tt.cpp -c”用 cl.exe (msvc 2019) 很好地编译。这是代码片段。

using void_t = void;

template <class _Ty, class = void>
struct _Add_reference { 
    using _Lvalue = _Ty;
    using _Rvalue = _Ty;
};


template <class _Ty>
struct _Add_reference<_Ty, void_t<_Ty&>> { 
    using _Lvalue = _Ty&;
    using _Rvalue = _Ty&&;
};

template <class _Ty>
using add_lvalue_reference_t = typename _Add_reference<_Ty>::_Lvalue;

template <class _Ty> 
constexpr bool _Is_copy_assignable_unchecked_v = __is_assignable_no_precondition_check(
    add_lvalue_reference_t<_Ty>, 
    add_lvalue_reference_t<const _Ty>
);

这是 cl.exe 的命令行:

>"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx86\x64\cl.exe" -c tt.cpp

当我使用 clang.exe ( 12.0.0 )将其转换为 llvm ir (或只是目标文件)时,我遇到了一些错误。这是带有 clang++.exe 的命令行,它发出一些错误消息。

>"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\Llvm\bin\clang++.exe" -c tt.cpp>"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\Llvm\bin\clang++.exe" -S -emit-llvm tt.cpp 错误信息:

tt.cpp:24:29: error: expected '(' for function-style cast or type construction
        add_lvalue_reference_t<_Ty>,
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
tt.cpp:26:1: error: expected '(' for function-style cast or type construction
);
^
2 errors generated.

对clang++.exe场景中的上述错误有什么意见吗?谢谢。

标签: c++windowstemplatesllvm-ircl.exe

解决方案


推荐阅读