error: C2061: syntax error: identifier 'concurrent_vector<`template-type-parameter-1',`template-type-parameter-2'>'

error: C2061: syntax error: identifier 'concurrent_vector<`template-type-parameter-1',`template-type-parameter-2'>'

代码 compiles/runs 在 Linux 和 macOS 上正常。在 Windows 10,我正在使用 Visual Studio 2017 工具链编译代码,但我收到此错误:

...\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h:680: error: C2061: syntax error: identifier 'concurrent_vector<'template-type-parameter-1','template-type-parameter-2'>'

concurrent_vector 模板发生错误:


    //! Copying constructor for vector with different allocator type
    template<class M>
    __TBB_DEPRECATED concurrent_vector( const concurrent_vector<T, M>& vector, const allocator_type& a = allocator_type() )
        : internal::allocator_base<T, A>(a), internal::concurrent_vector_base()
    {
        vector_allocator_ptr = &internal_allocator;
        __TBB_TRY {
            internal_copy(vector.internal_vector_base(), sizeof(T), &copy_array);
        } __TBB_CATCH(...) {
            segment_t *table = my_segment.load<relaxed>();
            internal_free_segments( table, internal_clear(&destroy_array), my_first_block.load<relaxed>() );
            __TBB_RETHROW();
        }
    }

错误发生在 TBB 内部 headers:

C:\...\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h

可能是什么原因?

这里是更完整的错误日志:

c:\users\m3\reposd-editor\editorlib\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h(680): error C2061: syntax error: identifier 'concurrent_vector<`template-type-parameter-1',`template-type-parameter-2'>'
c:\users\m3\reposd-editor\editorlib\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h(1167): note: see reference to class template instantiation 'tbb::concurrent_vector<T,A>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\include\memory_resource(860): note: see reference to class template instantiation 'std::pmr::_Intrusive_stack<std::pmr::monotonic_buffer_resource::_Header,void>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\include\memory_resource(465): note: see reference to class template instantiation 'std::pmr::_Intrusive_stack<std::pmr::unsynchronized_pool_resource::_Pool::_Chunk,void>' being compiled
c:\users\m3\reposd-editor\editorlib\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h(681): error C2334: unexpected token(s) preceding ':'; skipping apparent function body
c:\users\m3\reposd-editor\editorlib\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h(61): fatal error C1075: '{': no matching token found
jom: C:\Users\m3\repos\build-3dsceneeditor-Desktop_Qt_5_12_9_MSVC2017_64bit-Debug\editorlib\Makefile.Debug [debug\openvdbutils.obj] Error 2

结合 visual studio 2017 中的 /Zc:__cplusplus 编译器标志,此代码可用于重现问题:

#include <tbb/concurrent_vector.h>
int main()
{
    tbb::concurrent_vector<int> vector;
    vector.push_back(1);
    return 0;
}

正如 here 所讨论的,一个简短的修复是在 Windows 上降级到 TBB 2020 更新 1。感谢@AlanBirtles

这似乎是一个 visual studio 错误。来自 TBB github 存储库上的 issue /Zc:__cplusplus 编译器标志是原因。

启用该标志会导致 this code 使用 c++14 [[deprecated]] 属性:

    #if (__cplusplus >= 201402L)
        #define __TBB_DEPRECATED [[deprecated]]
        #define __TBB_DEPRECATED_MSG(msg) [[deprecated(msg)]]
    #elif _MSC_VER
        #define __TBB_DEPRECATED __declspec(deprecated)
        #define __TBB_DEPRECATED_MSG(msg) __declspec(deprecated(msg))
    #elif (__GNUC__ && __TBB_GCC_VERSION >= 40805) || __clang__
        #define __TBB_DEPRECATED __attribute__((deprecated))
        #define __TBB_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
    #endif

交换此代码以始终使用 visual studio 特定代码似乎可以解决问题:

    #if _MSC_VER
        #define __TBB_DEPRECATED __declspec(deprecated)
        #define __TBB_DEPRECATED_MSG(msg) __declspec(deprecated(msg))
    #elif (__cplusplus >= 201402L)
        #define __TBB_DEPRECATED [[deprecated]]
        #define __TBB_DEPRECATED_MSG(msg) [[deprecated(msg)]]
    #elif (__GNUC__ && __TBB_GCC_VERSION >= 40805) || __clang__
        #define __TBB_DEPRECATED __attribute__((deprecated))
        #define __TBB_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
    #endif

一个smaller demonstration同样的问题是:

template <typename T>
class Foo
{
public:
    Foo() {}

    template <typename X>
    [[deprecated]] Foo(const Foo<T>& a)
    {
    }
};

int main()
{
    Foo<int> x;
    return 0;
}

这仅在 Visual Studio 2017 年不起作用,it works in 2019 所以最简单的解决方法是更新 visual studio.

A pull request has been created to fix this issue and reported to Microsoft,我不希望 MS 修复它,因为它已经在 2019 年修复了。