0

I'm looking at this GitHub repository for a drop-in replacement for std::shared_ptr in Visual C++ 6 to modify some old code but I've found that this library is incompatible due a parser error being thrown by the compiler.

I know that nested template classes and friend classes are supported. But this is a template<class U> which is different from the template of class shared_ptr and also that the friend class is referencing itself.

What is the purpose of this self-referencing templated friend class and how is it being used?

The problem can be demonstrated without compiling the referenced shared_ptr library:

template<class T>
class shared_ptr
{
    template<class U>
    friend class shared_ptr;
};

int main() {
   return 0;
}

The compiler throws the following error:

main.cpp(5) : error C2059: syntax error : '<end Parse>'
        main.cpp(6) : see reference to class template instantiation 'shared_ptr<T>' being compiled
main.cpp(6) : error C2143: syntax error : missing ';' before '}'
        main.cpp(6) : see reference to class template instantiation 'shared_ptr<T>' being compiled
main.cpp(6) : error C2238: unexpected token(s) preceding ';'
        main.cpp(6) : see reference to class template instantiation 'shared_ptr<T>' being compiled

I don't need this library as I've been able to work around the issue by using Boost 1.34.1. But I would like to understand this limitation and what it purpose it serves.

Zhro
  • 2,546
  • 2
  • 29
  • 39
  • @Mgetz Are you sure? According to the [C++98 standard](http://www.lirmm.fr/~ducour/Doc-objets/ISO+IEC+14882-1998.pdf), both options are viable: _"There is no semantic difference between `class` and `typename` in a template-parameter." [temp.param/2]_ – Daniel Langr Oct 21 '21 at 14:39
  • 1
    @DanielLangr for VC++6, yes I probably should rephrase my comment but it's past the edit period at this point. Regardless it's a long since out of support compiler that never supported any standard in the first place. – Mgetz Oct 21 '21 at 14:41
  • I wrote a trivial test and can confirm that `class` and `typename` function identically within a template declaration when compiled with Visual C++ 6. The error is the same even when `typename` is used in the example provided in the question. – Zhro Oct 21 '21 at 14:46
  • @Zhro any specific reason you _must_ use VC++6? Are you supporting an old OS? – Mgetz Oct 21 '21 at 14:47
  • 2
    VC++ 6 is not C++98; it's pre-standardization. IIRC, the implementation of templates in particular was very "individual" at that point (they were a very recent addition to C++). – molbdnilo Oct 21 '21 at 15:09

0 Answers0