17

In this question it was discussed why exposing a private type with auto:

#include <iostream>

using namespace std;

class Base {

  class PrivateClass {    
  public: 
    void bar() { cout << "PrivateClass" << endl; }
  };

public:

  PrivateClass foo() {
    PrivateClass a;
    return a;
  }

};

int main() {

  Base b;
  auto p = b.foo();
  p.bar();
  return 0;
}

is perfectly fine by the C++11 standard. What I still don't get is how this idiom may be useful in a real application. Are there problems where this idiom can be effectively used, or it should be considered as a "curious" side-effect of the keyword?

Community
  • 1
  • 1
Massimiliano
  • 7,842
  • 2
  • 47
  • 62

2 Answers2

10

It can be useful if the return type is unspecified. For example, the returned object from a call to std::bind, or formerly boost::bind, is not specified. It is some implementation-defined functor, but you can't actually know its type without looking at implementation details. Before C++11's auto keyword, you could use boost::function as a variable type for storing the result of bind, or you could pass the result of bind to a function which takes a template argument. With C++11, you can store the result object using auto.

So, if a class has some internal type, it is not necessary to expose the actual type to a public API. A user of the class can simply use the auto keyword, and the API documentation can say the type is "unspecified". This keeps the actual internal type a private implementation detail, which can often improve encapsulation.

Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
  • 3
    I don't think that this really answers the question. It's a good answer to a different question, which is "what is the use of `auto`?" – Lightness Races in Orbit Dec 04 '12 at 18:46
  • 3
    I think it answers the question. The internal type name can be kept private while allowing the client to work with objects of that type. – Joseph Mansfield Dec 04 '12 at 18:49
  • 4
    @Lightness Races in Orbit, I'm not sure why you think this doesn't answer the question. He wants to know why someone would use `auto` to store a private type returned from a function. The answer is because this way the private type can be unspecified in the public API. – Charles Salvia Dec 04 '12 at 18:54
  • 1
    The answer did not say that when I wrote my comment. I really wish this FGIW would _stop_. – Lightness Races in Orbit Dec 04 '12 at 20:03
  • 1
    This question can aswell be "Exposing a private type with type deduction?" and be retagged to C++ (without -11 suffix). And this accepted answer applies just as-well to that question (passing the result to a function template). – Johannes Schaub - litb Dec 08 '12 at 23:28
1

Wheter the "auto" keyword & subclasing provides such feature, the concept of "private types" seems unpractical.

Before O.O.P., some developers hide some type using a (void*) pointer, to a "struct". Another more updated cases allowed an object of a given class, been exposed only by a superclass.

umlcat
  • 4,091
  • 3
  • 19
  • 29