Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deduce variable type #2

Open
paulpach opened this issue Jun 15, 2017 · 0 comments
Open

deduce variable type #2

paulpach opened this issue Jun 15, 2017 · 0 comments

Comments

@paulpach
Copy link

paulpach commented Jun 15, 2017

In C++11, it is recommended to deduce the type when possible by using auto, in some instances it is faster because it avoids type conversion, and it is easier to read when you declare several variables. For a longer explanation watch this video:

https://www.youtube.com/watch?v=xnqTKD8uD64&t=2474s
starting at minute 29

for example, this code:

    int n = strlen(http_str.c_str());
    int tot_len = n;
    while (n > 0) {
        int nwrite = send(_clt_sock_fd, http_str.c_str() + tot_len - n, n, 0);
        if (nwrite < n) {
            break;
        }
        n -= nwrite;
    }

could be done like this:

    auto n = strlen(http_str.c_str());
    auto tot_len = n;
    while (n > 0) {
        auto nwrite = send(_clt_sock_fd, http_str.c_str() + tot_len - n, n, 0);
        if (nwrite < n) {
            break;
        }
        n -= nwrite;
    }

In some cases auto is faster because it guarantees no type conversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant