转换操作符到Visual Studio 2013时遇到问题 [英] Trouble with cast operators converting to Visual Studio 2013

查看:90
本文介绍了转换操作符到Visual Studio 2013时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多年来,在Visual Studio 2005和2008中,我一直在使用简单的char缓冲区类。
我今天转换为VS 2013,并且在不同情况下遇到了模棱两可的转换错误。我已删除了不需要的代码部分:

I have been using a simple char buffer class for a number of years under Visual Studio 2005 and 2008. I converted to VS 2013 today and I am getting ambiguous conversion errors in different scenarios. I have removed unneeded portions of the code:

#include <string>

class ACP
{
public:
    ACP(const char* const init) : ourStr_(_strdup(init)), size_(strlen(init) + 1)  {    }
    virtual ~ACP() { free(ourStr_); }

    //
    // casting operators
    //
    operator char* ()                   { return ourStr_; }
    operator const char* const () const { return ourStr_; }

    operator const std::string() const  { return ourStr_; }

protected:
    // hide the default constructor
    ACP() : ourStr_(NULL), size_(0) {}
    // hide the copy constructor
    ACP(const ACP& copy);

protected:
    char* ourStr_;
    size_t size_;
}; // ACP

void t(const std::string& val)
{
    std::string s = val;
}
void test()
{
    const ACP acp("hello");
    std::string s = acp;
    const std::string cs = acp;
    t((std::string)acp);
    t(acp);
    char ch[50];
    strcpy(ch, acp);
}

新的冲突似乎在char *和std :: string之间演员。
这样的错误:

The new conflict seems to be between the char* and the std::string cast operators. Giving errors like so:

1>foo.cpp(36): error C2440: 'initializing' : cannot convert from 'const ACP' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
1>foo.cpp(37): error C2440: 'initializing' : cannot convert from 'const ACP' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
1>foo.cpp(38): error C2440: 'type cast' : cannot convert from 'const ACP' to 'std::string'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous


推荐答案

对于Visual Studio 2017,仅一行

With Visual Studio 2017, only the line

t((std::string)acp);

给出编译错误。

有很多方法可以修复该错误。有些人可能无法在VS 2013中使用。

There are many ways to fix that error. Some might not works with VS 2013.

t(std::move(acp));   // C++11
t(static_cast<const std::string &>(acp));

但是一般来说,强制转换不是一个好主意,因此添加方法 str() 到ACP可能是个好主意。或在 t 中添加一个需要 const ACP& 的重载。

But casting is generally not a good idea so adding a method str() to ACP might be a good idea. Or add an overload to t that takes a const ACP &.

如果可以使用支持C ++ 11的编译器,则 t 的重载将使用 std :: string&& ; 也可能有意义。

If you can use a compiler that support C++ 11, the an overload of t that takes a std::string && might make sense too.

这篇关于转换操作符到Visual Studio 2013时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆