如何解决错误:C2061:语法错误:标识符'concurrent_vector <`template-type-parameter-1',`template-type-parameter-2'>'
代码可以在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),©_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标头内:
C:\...\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h
可能是什么原因?
这是更完整的错误日志:
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
此代码可与Visual Studio 2017中的/Zc:__cplusplus
编译器标志一起用于重新创建问题:
#include <tbb/concurrent_vector.h>
int main()
{
tbb::concurrent_vector<int> vector;
vector.push_back(1);
return 0;
}
解决方法
这似乎是视觉工作室的错误。在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。
已创建一个pull request来解决此问题,reported to Microsoft我不希望MS对其进行修复,因为它已在2019年修复。
,如here所述,一个简短的解决方法是在Windows上降级为 TBB 2020 Update 1 。感谢@AlanBirtles
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。