Search code examples
c++syntax-errortbb

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


A code compiles/runs fine on Linux and macOS. On Windows 10, I'm compiling the code with Visual Studio 2017 toolchain, but I'm receiving this error:

...\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'>'

The error happens at this concurrent_vector template:


    //! 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();
        }
    }

The error happens inside the TBB headers:

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

What could possibly be the cause?

Here is a more complete error log:

c:\users\m3\repos\3d-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\repos\3d-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 Studio\2017\Community\VC\Tools\MSVC\14.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 Studio\2017\Community\VC\Tools\MSVC\14.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\repos\3d-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\repos\3d-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

This code can be used to recreate the issue in combination with the /Zc:__cplusplus compiler flag in visual studio 2017:

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

Error log


Solution

  • This seems to be a visual studio bug. From the issue on the TBB github respository the /Zc:__cplusplus compiler flag is the cause.

    Enabling that flag causes this code to use the c++14 [[deprecated]] attribute:

        #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
    

    Swapping this code around to always use the visual studio specific code seems to fix the problem:

        #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
    

    A smaller demonstration of the same problem is:

    template <typename T>
    class Foo
    {
    public:
        Foo() {}
    
        template <typename X>
        [[deprecated]] Foo(const Foo<T>& a)
        {
        }
    };
    
    int main()
    {
        Foo<int> x;
        return 0;
    }
    

    This only doesn't work in Visual Studio 2017, it works in 2019 so the simplest fix is to update visual studio.

    A pull request has been created to fix this issue and reported to Microsoft, I wouldn't expect MS to fix it though as it is already fixed in 2019.