PeterFeicht / cppreference-doc

C++ standard library reference
en.cppreference.com
GNU General Public License v3.0
576 stars 47 forks source link

About basic_string_view substr example code #26

Closed lijh8 closed 2 years ago

lijh8 commented 2 years ago

Hi,

The example code here: https://en.cppreference.com/w/cpp/string/basic_string_view/substr

uses assignment expressions as arguments: pos = 2, count = 3

It is very confusing, looks like positional parameter or named parameter in Python or Swift. Can we make it simpler?

How can I find the source file in this repo, for the corresponding html web page: ../html-book-20220201/reference/en/cpp/string/basic_string_view/substr.html

Thanks

After:

    std::cout << data.substr() << '\n'; // ABCDEF
    std::cout << data.substr(1) << '\n'; // BCDEF
    std::cout << data.substr(2, 3) << '\n'; // CDE

    std::cout << data.substr(4, 42) << '\n'; // EF
        // count -> 2 == size() - pos == 6 - 4

    try {
        data.substr(666, 1); // throws: pos > size()

Before:

    std::cout << data.substr(pos = 1) << '\n'; // BCDEF
    std::cout << data.substr(pos = 2, count = 3) << '\n'; // CDE

    std::cout << data.substr(pos = 4, count = 42) << '\n'; // EF
        // count -> 2 == size() - pos == 6 - 4

    try {
        data.substr(pos = 666, count = 1); // throws: pos > size()
PeterFeicht commented 2 years ago

This repository contains code to create an offline copy of cppreference.com, it doesn't contain the source code for cppreference.com (which is a wiki).

I agree with you that example code should be valid C++, feel free to contribute your changes to cppreference.com.