小编Jon*_*asW的帖子

C++:为什么gcc在访问operator []时更喜欢非const over const?

关于C++,这个问题可能更适合问题,但是因为我在linux上使用gcc就是上下文.考虑以下程序:

#include <iostream>
#include <map>
#include <string>

using namespace std;

template <typename TKey, typename TValue>
class Dictionary{
    public:
    map<TKey, TValue> internal;

    TValue & operator[](TKey const & key)
    {
        cout << "operator[] with key " << key << " called " << endl;
        return internal[key];
    }

    TValue const & operator[](TKey const & key) const
    {
        cout << "operator[] const with key " << key << " called " << endl;
        return internal.at(key);
    }

};

int main(int argc, char* argv[])
{
    Dictionary<string, …
Run Code Online (Sandbox Code Playgroud)

c++ gcc const

7
推荐指数
1
解决办法
875
查看次数

标签 统计

c++ ×1

const ×1

gcc ×1