Boost logo

Boost :

Subject: Re: [boost] Preprocessor directive for a sequence that differs only in datatype
From: Larry Evans (cppljevans_at_[hidden])
Date: 2017-07-11 11:17:42


On 07/11/2017 03:09 AM, Rishabh Arora via Boost wrote:
> Thank you, Gavin Lambert.
> I don't think any of the preprocessor directives will work then? because
> type cannot be determined at comile time? Is there any other way to do
> that?
>
What about replacing the switch with an index into a vector
of function pointers? Something like:

#include <iostream>
template < class T >
void print_column(std::size_t col) {
   T dummy;
   std::cout <<dummy<< std::endl;
   return;
}
using fptr=void (*)(std::size_t);
fptr printers[]=
{ print_column<int>
, print_column<float>
};
int main()
{
   unsigned ncol_=2;
   for(unsigned i=0; i<ncol_; ++i)
   {
     printers[i](0);
   }
   return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk