C2660: _splitpath_s在Visual Studio 2013中因std::array错误而失败
C2660: _splitpath_s fails with std::array error in Visual Studio 2013
Visual Studio 2013在语言数组上有点奇怪,在全局函数中允许将其初始化为char result[100] = { 0 };
,但如果它是类的成员则不允许解决方法C2536:不能为数组指定显式初始化器,在Visual Studio 2013中,int m_array[3];
在class A
, A() :m_array{ 0, 1, 2 } {}
失败错误C2536: "'A::A::m_array':不能为数组指定显式初始化器"。
在同一篇文章中,建议使用代替std::array<int, 3> m_array;
并初始化A() : m_array ({ 0, 1, 2 }) {}
, IDE红色下划线"0"提示"错误:此子对象初始化项不能省略大括号。"但可以编译。
更好的是,一条注释建议使用一对额外的大括号A() : m_array ({ { 0, 1, 2 } }) {}
,现在一切顺利!
将std::array
传递给需要char *
参数的函数,std::数组优于c风格数组,建议使用my_array.data()
,其中my_array
是std::array
。
现在我遇到了_spitpath_s
的问题:
传统的char *
样式编译_splitpath_s(fullpathfilename, drive, dir, name, ext)
,其中参数均为char
数组;但是使用std::array
会触发错误C2660:
class B2
{
public:
const int MAX_LEN = 200;
std::array<char, 200> drive, dir, name, ext;
B2() :drive({ { 0 } }), dir({ { 0 } }), name({ { 0 } }), ext({ { 0 } }) {}
void split(const char * fullpathfilename) {
_splitpath_s(fullpathfilename, drive.data(), dir.data(), name.data(), ext.data()); //error C2660: '_splitpath_s' : function does not take 5 arguments
}
};
.
为什么_splitpath_s
在这里失败?这是一个旧的C风格函数,在stdlib.h
中定义,如果在c++中有解决方法,也是可以接受的。
_splitpath_s
的5个参数版本是一个模板函数,期望一个字符指针作为输入路径,另外4个是c风格的字符数组。因为你传递的是c++ array
对象,模板不会生成,而且由于SFINAE,它不可用,所以没有接受5个参数的函数。
要使用它,您必须使用9个参数版本,其中您传入输入地址和缓冲区大小。
- C++11 中不同类型的对象的 std::array 的替代方案
- constexpr begin of a std::array
- C++如果必须在编译时确定大小,std::array 有什么意义?
- 标准::unordered_map 中的 std::array 的值初始化
- 确保编译时的特定 std::array 位置
- std::array的长度有大小限制吗?
- 将 std::array 移动到另一个 std::array
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- std::bind on statd::array 的运算符 []
- 检查输入 std::array 指针数据是否等于某个常量数组
- 如何读/写或遍历 std::array 中的特定元素范围?
- 是否可以使用 std::array 作为 POD 结构的数据容器?
- 如何在C++中传递一个大小未知的 std::array?
- 在现代C++中将 std::array<std:::array<T,N>> 转换为 std::vector<T>
- 在 C++ 中访问 std::array<std::array> 元素
- 将标量+std::array+std::tuple转换为一个大元组
- 如何初始化 std::array<std::array<T, 2>, 2> 的对象?
- 初始化 std::p air<double, std::array<std::p air<double, double>, 3> >
- 单独移动数组(raw array, std::array, std::vector)的每个元素
- 包装方法返回 c++ std::array<std::string, 4> in cython