|
Boost : |
Subject: Re: [boost] Preprocessor directive for a sequence that differs only in datatype
From: Larry Evans (cppljevans_at_[hidden])
Date: 2017-07-16 15:32:31
On 07/11/2017 06:17 AM, Larry Evans via Boost wrote:
> 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;
> }
>
More specifically:
#ifdef USE_PRINTER_TYPES
template<typename... Types>
struct printers
{
using fptr = void(data_frame::*)(size_t);
static constexpr fptr _[sizeof...(Types)]=
{ &data_frame::print_column<Types>...
};
};
using printer_types=printers<COLUMN_DATA_TYPES>;
#endif//USE_PRINTER_TYPES
Then, use printer_types in the data_frame::print function as:
std::cout << "[" << column_headers_(i) << "]" << ": ";
auto const itype=base_::operator[](column_headers_(i)).type();
#ifdef USE_PRINTER_TYPES
printer_types::fptr printer=printer_types::_[itype];
(this->*printer)(i);
#else
switch(itype) {
.
.
.
#endif//USE_PRINTER_TYPES
HTH.
-regards,
Larry
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk