C++ name resolution for member functions in template class -


#include <iostream>  template<class t> struct {     typedef t a;  };  template<class t>  struct b {     typedef typename a<t>::a a;     static foo(a b);  };  template<class t>  b<t>::foo(a b) {return b}  int main() {     std::cout << b<int>::foo(1);  } 

gives following error: (try it).

main.cpp:13:1: error: 'a' not name type     b<t>::foo(a b) {return b} 

an inline definition not suffer error.

could please explain why compiler can not resolve a in case, andhow can make code work.

i not resolve names explicitly like

typename b<t>::a b<t>::foo(typename b<t>::a b) {return b} 

as decrease readability.

that's because a here still looking in global scope:

template<class t>  b<t>::foo(a b) {return b;} ^^ 

you're doing unqualifed lookup on a. once b<t>:: part of definition, scope added further lookup. type of argument b looked in scope of b<t>.

you need qualify return type:

template<class t>  typename b<t>::a b<t>::foo(a b) {return b;} 

the relevant rules why argument type a can found in [basic.lookup.unqual]/8:

for members of class x, name used in member function body, in default argument, in exceptionspecification, in brace-or-equal-initializer of non-static data member (9.2), or in definition of class member outside of definition of x, following member’s declarator-id, shall declared in 1 of following ways:
— before use in block in used or in enclosing block (6.3), or
— shall member of class x or member of base class of x (10.2), or

the return type a not match bolded text (or of text above), argument type a does.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -