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

the right way to use 'auto' keyword #1727

Closed
AlexStocks opened this issue Jul 14, 2023 · 0 comments
Closed

the right way to use 'auto' keyword #1727

AlexStocks opened this issue Jul 14, 2023 · 0 comments

Comments

@AlexStocks
Copy link
Collaborator

AlexStocks commented Jul 14, 2023

Describe the bug
A clear and concise description of what the bug is.

#include <string>
#include <iostream>
#include <vector>

using namespace std;

template <class T>
constexpr
std::string_view
type_name()
{
    using namespace std;
#ifdef __clang__
    string_view p = __PRETTY_FUNCTION__;
    return string_view(p.data() + 34, p.size() - 34 - 1);
#elif defined(__GNUC__)
    string_view p = __PRETTY_FUNCTION__;
#  if __cplusplus < 201402
    return string_view(p.data() + 36, p.size() - 36 - 1);
#  else
    return string_view(p.data() + 49, p.find(';', 49) - 49);
#  endif
#elif defined(_MSC_VER)
    string_view p = __FUNCSIG__;
    return string_view(p.data() + 84, p.size() - 84 - 7);
#endif
}

/**
 * description   : to .<br/><br/>
 *
 * @val          : value
 **/
class A
{
    public:
        A() {}
        ~A() {}

        vector<int>& vref() {
            return v_;
        }

        vector<int>* vpoint() {
            return &v_;
        }

        void output() {
            for (auto i : v_) {
                cout << i << endl;
            }
        }

    private:
        vector<int> v_;
};

int main(int argc, char** argv)
{
    A ca;

    cout << "ref:" << endl;
    auto &v = ca.vref();
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    ca.output();
    std::cout << type_name<decltype(v)>() << endl;

    cout << "value:" << endl;
    auto vv = ca.vref();
    vv.push_back(4);
    ca.output();
    std::cout << type_name<decltype(vv)>() << endl;

    cout << "point ref:" << endl;
    auto *vpr = ca.vpoint();
    vpr->push_back(5);
    ca.output();
    std::cout << type_name<decltype(vpr)>() << endl;

    cout << "point value:" << endl;
    auto vpv = ca.vpoint();
    vpv->push_back(6);
    ca.output();
    std::cout << type_name<decltype(vpv)>() << endl;

    // cout << "point value ref:" << endl;
    // [a.cc](http://a.cc/):90:11: error: non-const lvalue reference to type 'std::vector<int> *' cannot bind to a temporary of type 'vector<int> *'
    // auto &vpvr = ca.vpoint();
    // vpvr->push_back(7);
    // ca.output();
    // std::cout << type_name<decltype(vpvr)>() << endl;

    return 0;
}

output

image

@AlexStocks AlexStocks changed the title the the right way to use 'auto' keyword Jul 14, 2023
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