Need some clear code to implement the template

I am working on a C ++ library with some template class and method (almost only templates). I wanted to separate the declaration and implementation for easier reading. But sometimes it just gets crazy:

template < typename T >
class Foo {

  template < typename U >
  class SubFoo {

  };

  template < typename U >
  SubFoo<U> bar();
};

template < typename T >
template < typename U >
Foo<T>::SubFoo<U> Foo<T>::bar() {}

And this is really a minimal example ... Therefore, if you have an alias to improve this, or any style guide style. I work with NeoVim, so if you know some plugins that can make life easier, that would be fine too.

Ps: I have no restrictions on C ++.


In fact, I forget that in C ++, there are some basic things, such as a macro.

#define Class Foo<T>

template < typename T >
template < typename U >
Class::SubFoo<U> Class::bar() {}

Not very nice, but still better. Anyway, to do this without a macro ?

+4
2

, , .

(, ):

template < typename Object, bool Shared >
  template < ECS_INT Filter >
DefaultManager<Object, Shared>::iterator<Filter>  DefaultManager<Object, Shared>::begin() {

  unsigned int i;
  for (i = 0;
       !iterator<Filter>::is_Valid(m_pool[i].data);
       ++i);
  return std::move(iterator<Filter>(i));
}

template < typename Object, bool Shared >
  template < ECS_INT Filter >
DefaultManager<Object, Shared>::iterator<Filter>  DefaultManager<Object, Shared>::end() {

  unsigned int i;
  for (i = m_pool.size();
       !iterator<Filter>::is_Valid(m_pool[i].data);
       --i);
  return std::move(iterator<Filter>(i));
}

#define TEMPLATE_ARGS  typename Object, bool Shared
#define CLASS    DefaultManager<Object, Shared>
#define ITERATOR CLASS::iterator<Filter>

template < TEMPLATE_ARGS >
  template < ECS_INT Filter >
ITERATOR  CLASS::begin() {

  unsigned int i;
  for (i = 0;
       !ITERATOR::is_Valid(m_pool[i].data);
       ++i);
  return std::move(ITERATOR(i));
}

template < TEMPLATE_ARGS >
  template < ECS_INT Filter >
ITERATOR  CLASS::end() {

  unsigned int i;
  for (i = m_pool.size();
       !ITERATOR::is_Valid(m_pool[i].data);
       --i);
  return std::move(ITERATOR(i));
}

, , , , . , .

, , , , .

0

, , . , - .

, , " - ", / .

,

1) ( "header.h" ), struct foo , ,

template <typename T>
struct foo
 { 
   foo ();
 };

2) cpp ( "a.cpp" ) main(), ​​ foo<int>

#include "header.h"

int main ()
 {
   foo<int>  fi;
 }

3) cpp ( "b.cpp" ), foo

#include "header.h"

template <typename T>
foo<T>::foo ()
 { }

, ?

( clang++ -c -o a.o a.cpp clang++ -c -o b.o b.cpp) (clang++ -o a.out a.o b.o), : undefined foo<int>::foo()

?

- , "b.cpp" , , "a.cpp" ​​ foo<int>, foo<int>::foo().

, , , "b.cpp" , , foo<int>, "b.cpp"

#include "header.h"

template <typename T>
foo<T>::foo ()
 { }

template struct foo<int>;

foo<long> , "b.cpp" .

(IMHO), , foo<T>.

(IMHO) struct/class (IMHO, ), /

0

Source: https://habr.com/ru/post/1662719/


All Articles