简体   繁体   English

重新定义<运算符以在字符串的stl算法中使用

[英]redefine < operator to use in stl algorithms for strings

Is it possible to redefine operator < for strings without modifying std namespace, to make this operator use in standard algorithms? 是否可以在不修改std名称空间的情况下重新定义operator < for strings,以使此运算符在标准算法中使用? For example, I can write: 例如,我可以写:

namespace std
{

    bool operator <(const std::string & rhs, const std::string & lhs)
    {
        std::cout << "lol";
        return false;
    }

}

int main()
{
    std::vector<std::string> lol = { "a", "b", "ba", "aa" };
    std::sort(lol.begin(), lol.end());
}

and "lol" will be printed several times. 并且“lol”将被打印几次。 But if I move operator < outside from std namespace, default operator < will be used and nothing will be printed. 但是如果我从std命名空间移动operator < outside,将使用default operator <并且不会打印任何内容。 Is it possible to make std::sort using custom operator < without including it to std namespace? 是否可以使用自定义operator <进行std::sort而不将其包含到std命名空间?

Yes I know, I can pass another comparator to std::sort but it's interesting for me if I could do what I asked and how? 是的,我知道,我可以将另一个比较器传递给std :: sort但是如果我可以按照我的要求做什么怎么办呢?

Also am I correct, that it's correct to add such template specialization to std namespace? 我也是正确的,将这样的模板特化添加到std命名空间是正确的吗?

Update: This is not practical question, I just want to know how can I do that if it's possible. 更新:这不是一个实际问题,我只是想知道如果可能的话我该怎么做。

No, it is not. 不它不是。 Adding a function to the standard namespace is undefined behavior. 向标准命名空间添加函数是未定义的行为。 [namespace.std]/1 states: [namespace.std] / 1状态:

The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. 如果C ++程序向命名空间std或命名空间std中的命名空间添加声明或定义,则它是未定义的,除非另有说明。 A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited. 只有当声明取决于用户定义的类型并且特化符合原始模板的标准库要求且未明确禁止时,程序才可以将任何标准库模板的模板特化添加到命名空间std。

If you want to change how std::sort sorts then you can provide a lambda and define what you want 如果你想改变std::sort排序方式,那么你可以提供一个lambda并定义你想要的东西

std::sort(std::begin(foo), std::end(foo), [](const auto& lhs, const auto& rhs) { /* your code here */ });

Is it possible to redefine operator < for strings without modifiying std namespace 是否可以在不修改std命名空间的情况下重新定义operator <for strings

You can define the overload in another namespace, sure. 您可以在另一个命名空间中定义重载。 But as you have found out, it will not be found by overload resolution unless explicitly qualified. 但是,正如您已经发现的那样,除非明确限定,否则将无法通过重载决策找到它。

Is it possible to make std::sort using custom operator < without including it to std namespace? 是否可以使用自定义运算符<进行std :: sort而不将其包含到std命名空间?

Yes, and you already seem to know how: 是的,你似乎已经知道如何:

Yes I know, I can pass another comparator to std::sort 是的我知道,我可以将另一个比较器传递给std :: sort

This is exactly what the comparator argument is for. 这正是比较器参数的用途。

Also am I correct, that it's corect to add such template specialization to std namespace? 我也是正确的,将这种模板专业化添加到std命名空间的核心是什么?

That is not a template specialization; 那不是模板专业化; It is a function definition and you may not add function definitions to std namespace - or else the behaviour is undefined. 它是一个函数定义,您可能不会将函数定义添加到std命名空间 - 否则行为是未定义的。 You would be allowed to add template specializations, but only if at least one type argument is a user defined type. 您可以添加模板特化,但前提是至少有一个类型参数是用户定义的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM