为什么编译器不执行类型转换?

发布于 2024-11-25 08:55:07 字数 613 浏览 1 评论 0原文

考虑以下代码。

#include <iostream>
#include <string>

struct SimpleStruct
{
    operator std::string () { return value; }
    std::string value;
};

int main ()
{
    std::string s;    // An empty string.
    SimpleStruct x;   // x.value constructed as an empty string.

    bool less = s < x; // Error here.
    return 0;
}

此代码无法在 g++ 或 Microsoft Visual C++ 上编译。编译器给出的错误报告是 no match for operator '<'在 的 < x'。问题是为什么编译器不简单地根据给定的operator string ()SimpleStruct x转换为string,然后使用运算符 < (字符串,字符串)

Consider the following code.

#include <iostream>
#include <string>

struct SimpleStruct
{
    operator std::string () { return value; }
    std::string value;
};

int main ()
{
    std::string s;    // An empty string.
    SimpleStruct x;   // x.value constructed as an empty string.

    bool less = s < x; // Error here.
    return 0;
}

This code does not compile either on g++ or Microsoft Visual C++. The error report given by compilers is no match for operator '<' in 's < x'. The question is why does the compiler not simply convert the SimpleStruct x to string according to the given operator string () and then use operator < ( string, string )?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱桃奶球 2024-12-02 08:55:07

std::stringoperator< 是一个函数模板。重载是:

  template<class charT, class traits, class Allocator>
    bool operator< (const basic_string<charT,traits,Allocator>& lhs,
            const basic_string<charT,traits,Allocator>& rhs);
  template<class charT, class traits, class Allocator>
    bool operator< (const basic_string<charT,traits,Allocator>& lhs,
            const charT* rhs);
  template<class charT, class traits, class Allocator>
    bool operator< (const charT* lhs,
            const basic_string<charT,traits,Allocator>& rhs);

您的调用与任何可用的重载都不匹配,因此它们都从候选列表中删除。由于没有选择函数模板作为解析调用的候选函数,因此没有任何内容可以将 SimpleStruct 转换为。

template <class T>
class String
{
};

template <class T>
bool operator< (const String<T>&, const String<T>&) { return true; }


//if a suitable non-template function is available, it can be picked
//bool operator< (const String<char>&, const String<char>&) { return true; }

struct SimpleStruct
{
   operator String<char> () { return value; }
   String<char> value;
};

int main()
{
    String<char> s;
    SimpleStruct ss;
    s < ss; //the call doesn't match the function template, leaving only the commented-out candidate
}

operator< for std::string is a function template. The overloads are:

  template<class charT, class traits, class Allocator>
    bool operator< (const basic_string<charT,traits,Allocator>& lhs,
            const basic_string<charT,traits,Allocator>& rhs);
  template<class charT, class traits, class Allocator>
    bool operator< (const basic_string<charT,traits,Allocator>& lhs,
            const charT* rhs);
  template<class charT, class traits, class Allocator>
    bool operator< (const charT* lhs,
            const basic_string<charT,traits,Allocator>& rhs);

Your call doesn't match any of the available overloads, so they are all removed from a list of candidates. Since no function template was picked as a candidate for resolving the call, there is nothing to convert SimpleStruct to.

template <class T>
class String
{
};

template <class T>
bool operator< (const String<T>&, const String<T>&) { return true; }


//if a suitable non-template function is available, it can be picked
//bool operator< (const String<char>&, const String<char>&) { return true; }

struct SimpleStruct
{
   operator String<char> () { return value; }
   String<char> value;
};

int main()
{
    String<char> s;
    SimpleStruct ss;
    s < ss; //the call doesn't match the function template, leaving only the commented-out candidate
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文