auto seems to return wrong type

I am trying out a technique for a singleton class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// access controlled singleton, accessed through function "instance()"
// singleton is constructed in this function
// so that constructor and destructor will be used
class single
{
  // private constructor/destructor
  private:
  single(void) { std::cout << "ctor\n";  }
  ~single(void){ std::cout << "dtor\n"; }

  public:
  
  // access to the instance of the class
  static single& instance(void)
  {
    static single the_instance;
    return the_instance;
  }
};


Playing around with the code in main(), I am having trouble with auto:
1
2
single& s = single::instance();  // works fine
auto    a = single::instance();  // error ~single() is private 


When I make the destructor public, the output of the program is:
1
2
3
ctor
dtor
dtor


Whats going on here?

Thanks

Edit: So I fixed this by typing auto&. I'm still confused though, why wouldn't auto know I am returning a reference?
Last edited on
auto strips off top level & && and const. There are good reasons for this I just can't remember them off the top of my head without coffee.
http://thbecker.net/articles/auto_and_decltype/section_03.html

Has some info I think (haven't read it all the way through though.
Topic archived. No new replies allowed.