C2660: _splitpath_s在Visual Studio 2013中因std::array错误而失败

C2660: _splitpath_s fails with std::array error in Visual Studio 2013

本文关键字:array std 错误 失败 中因 Studio splitpath Visual C2660 2013      更新时间:2023-10-16

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_arraystd::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个参数版本,其中您传入输入地址和缓冲区大小。