mar*_*tin 2 tuples vector c++11 visual-studio-2013
我想在 std::vector 中存储三个任意整数,而不定义结构/类。所以我选择了 std::tuple<>:
std::vector<std::tuple<unsigned int, unsigned int, unsigned int>
Run Code Online (Sandbox Code Playgroud)
使用MS VS 2013,会导致以下错误:
>c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(1628): error C2036: 'std::tuple<unsigned int,unsigned int,unsigned int> *' : unknown size
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(1622) : while compiling class template member function 'void std::vector<std::tuple<unsigned int,unsigned int,unsigned int>,std::allocator<_Ty>>::_Tidy(void)'
1> with
1> [
1> _Ty=std::tuple<unsigned int,unsigned int,unsigned int>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(945) : see reference to function template instantiation 'void std::vector<std::tuple<unsigned int,unsigned int,unsigned int>,std::allocator<_Ty>>::_Tidy(void)' being compiled
1> with
1> [
1> _Ty=std::tuple<unsigned int,unsigned int,unsigned int>
1> ]
1> d:\projects\gl33\src\nvf.cpp(39) : see reference to class template instantiation 'std::vector<std::tuple<unsigned int,unsigned int,unsigned int>,std::allocator<_Ty>>' being compiled
1> with
1> [
1> _Ty=std::tuple<unsigned int,unsigned int,unsigned int>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =======
Run Code Online (Sandbox Code Playgroud)
这是由于 MSVS2013 编译器的限制吗?或者我做错了什么?
如果类型仅被前向声明但未定义,则类类型是已知的(其名称已知),但其大小是未知的。例如
struct X;
sizeof(X) // error: X is incomplete
Run Code Online (Sandbox Code Playgroud)
类型的大小对于指针算术很重要,这是查看编译器错误(其中提到指向 的指针tuple
)时的另一个提示。
MSDN为 C2036 提供了以下示例:
struct A* pA;
int main() {
pA++; // C2036, size of A not known
((char*&)pA)++; // OK, if sizeof(A) == sizeof(char)
}
Run Code Online (Sandbox Code Playgroud)
其中struct A* pa
隐式前向声明struct A
.
当您自己没有包含所有必需的标头时,标准库的标头可能会发生这种情况。标准库中的类型之间存在相互依赖性。如果标准库标头仅需要 的前向声明tuple
,则它不会包含重量级tuple
标头本身,以减少编译时间。
<vector>
我可以通过仅包含但不包含来重现OP中的问题<tuple>
。解决方案:手动包含您需要类型的所有标头 - 使用 时vector<tuple<..>>
,包含<tuple>
(以及<vector>
)。一般来说,包含标头可以保证特定类型集的可用性。为了最大限度地提高可移植性,请始终确保您包含的标头保证您可以在程序中使用所有类型(*)。
(*)更具体地说,您应该确保您的程序需要定义的所有类型都有定义。标准库容器要求其值类型是完整的(至少在类模板实例化时)。因此,如果您的程序需要 的定义vector<tuple<unsigned, unsigned>>
,那么它也需要 的定义tuple<unsigned, unsigned>
。
归档时间: |
|
查看次数: |
1713 次 |
最近记录: |