Z
Z
Zellily2017-10-05 22:36:10
C++ / C#
Zellily, 2017-10-05 22:36:10

The template argument is a structure type. How can this type be replaced other than #define NEW_TYPE OLD_TYPE?

Example:

template<typename T> int funcExample(int x, int y) { T s; s.a = x; s.b = y; return s.a + s.b; }

struct Foo{
   int a;
   int b;
};

#define Bar Foo
  
int main()
{
   int absum  = funcExample<Foo>(1, 2);
   std::cout << "sum: " << absum << "\n";
  
   absum  = funcExample<Bar>(3, 4);
   std::cout << "sum: " << absum << "\n";  
}

The fact is that I have a lot of all sorts of Foo, Bar, etc. for code readability. They are exactly the same in appearance, but different in use.
Or I'll make an unreadable code (well, with comments that this is - this is actually that). Or is there a solution that I don't know about. With some pointers, for example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
devalone, 2017-10-05
@Zellily

en.cppreference.com/w/cpp/language/type_alias

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question