2

I am quite confused when I come across this piece of code (from Asio)

template <typename>
struct associated_allocator_check
{
  typedef void type;
};

There seems to be no template argument. I can't find similar code from my C++ books. I would be grateful if someone can explain this to me.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Cheung Brian
  • 715
  • 4
  • 11
  • 29

1 Answers1

1

Take a look at the following question which refers to unnamed function parameters in C++/C.

Similarly in your situation, the template argument is unused and thus to avoid a compiler warning about an 'unused template argument' , you simply do not explicitly name it...problem solved.

  • So is it a good programming practice that you do something just to avoid compiler warning? – Cheung Brian Aug 05 '18 at 03:22
  • Sometimes you have a parameter that's required by an interface but the function doesn't use it. Maybe the parameter is no longer necessary, is only necessary in other functions that must use the same signature (especially so they can be called through pointers) or the functionality hasn't been implemented yet. When these situations arise, it is good practice to use this method to avoid compiler warnings. Comments should also be added to highlight that the unused parameter is intentional and not accidental. If the unused parameter is truly extraneous, then just leave it out. – GenericDeveloperProfile Aug 05 '18 at 03:26
  • 1
    @CheungBrian Compiler warnings are the compiler telling you that you may have made a mistake. If you have not made a mistake, it's best to adjust the code to eliminate the warning and make it easier to spot warnings for mistakes you did make. Any time you see a warning, address, not ignore, it. – user4581301 Aug 05 '18 at 04:37