News for hackers. Stuff that matters.
Re:Usability improvements in GCC 8 by vmarsy on Thursday March 15, 2018 @08:40PM (#16597743)
A lot of good stuff, reading C++ errors is like learning an extra language on top of the C++ language, I can't wait to try it out.

> How do I get at some private field?

That one is interesting! I wonder how smart it is.

For instance in the following example:

    class foo
    {
    public:
      std::pair<int, int> get_coordinates() const { return std::make_pair(m_x, m_y); }
    
    private:
      int m_x;
	  int m_y;
    };
    
    
    void test(foo *ptr)
    {
      if (ptr->m_x >= 3)
        ;// etc
    }

I wonder if the compiler would be able to figure out that m_x is accessible via `ptr->get_coordinates().first` ?
Re:Usability improvements in GCC 8 by dmalcolm on Thursday March 15, 2018 @09:50PM (#16598005)
[author of the blog post here]

Sadly, gcc 8 isn't smart enough to do that yet; I only implemented it for simple "return name_of_field;" accessors.

But it's an interesting idea, which I've filed as: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84895 - thanks.

Are there other cases where idiomatic C++ might lead to such an indirect suggestion being appropriate for accessing the field? (std::tuple, I suppose, but I'm not sure how often that would arise in practice...)

Re:Usability improvements in GCC 8 by vmarsy on Friday March 16, 2018 @05:46PM (#16604255)
thanks for taking time to reply!

Having some modest compilers knowledge, I'm already impressed that the first use case is possible. It's already covering many uses cases where people use simple Getters, so thanks for the work.

Yes returning std::tuples of an object state or something might be a valid use case, but as you say, I'm not sure how often this would occur!

Re:Usability improvements in GCC 8 by vidarh on Thursday March 15, 2018 @09:52PM (#16598012)
Looks like that's too complicated. This is what godbolt.org shows the output at (if you add the missing #include <utility> and pick "trunk" for gcc):

    <source>: In function 'void test(foo*)':
    <source>:16:16: error: 'int foo::m_x' is private within this context
           if (ptr->m_x >= 3)
                    ^~~ 
    <source>:9:11: note: declared private here
           int m_x;
               ^~~
    Compiler returned: 1