1

The following code compiles without any error:

class foo
{
public:
  void some_func() {}
};

class bar
{
private:
  foo instance;

public:
  auto some_func() -> decltype(instance.some_func()) {}
};

int main() {}

While the following code doesn't compile in MSVC-12.0:

class foo
{
public:
  void some_func() {}
};

class bar
{
private:
  foo instance;

public:
  auto some_func() -> decltype(instance.some_func());
};

auto bar::some_func() -> decltype(instance.some_func()) {}

int main() {}

It gives me the following errors:

error C2228: left of '.some_func' must have class/struct/union
error C2556: 'int bar::some_func(void)' : overloaded function differs only by return type from 'void bar::some_func(void)'
 : see declaration of 'bar::some_func'
error C2371: 'bar::some_func' : redefinition; different basic types
 : see declaration of 'bar::some_func'

What am I doing wrong? How can I fix it?

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • 1
    clang++3.5 and g++4.8.1 accepts it. This is a strong hint that's a bug in MSVC. – dyp Feb 26 '14 at 10:26
  • 1
    I think it's really a bug in MSVC. The *trailing-return-type* is not part of the *declarator-id*, but follows it. Therefore, the name lookup is (= should be) the same as *inside* the definition of the body of the member function. – dyp Feb 26 '14 at 10:36

1 Answers1

0

I don't have VS12 right now, so I'm a little bit guessing. At the declaration of bar::some_func, when declared outside the class, you don't have access to 'instance' member.

try something like:

auto bar::some_func() -> decltype(bar::instance.some_func()) {}
Uri London
  • 10,631
  • 5
  • 51
  • 81