C++11 feature std::map::at compiling on older versions of C++

Viewed 231

The code below uses std::map::at which was introduced in c++11, however, while specifying an older version (I've tried -std=c++03, -std=c++0x and -std=c++98) in g++ and cmake, it still gets compiled. Other c++11 features do not work, such as range-based for loop.

#include <iostream>
#include <map>

int main() {
    std::map<int, int> my_map;
    my_map[0] = 1;
    std::cout << my_map.at(0);
    return 0;
}

g++ command:

g++ -std=c++03 -o main *.cpp

CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)
project(test)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++03")
add_executable(main main.cpp)

Output in both cases:

1

What could be the issue here? Thanks in advance.

1 Answers

According to discussion at https://en.cppreference.com/w/Talk:cpp/container/map/at indeed std::map::at was missing in the original C++98 standard

Technically std::map::at appeared in Library Working Group LWG 464 and adopted into the 2008 standard draft. On the other hand, it seems compilers did implement LWG 464 as a defect report. Clang, GCC, MSVC - these are all enabled it in C++98 mode.

Related