ben-strasser / fast-cpp-csv-parser

fast-cpp-csv-parser
BSD 3-Clause "New" or "Revised" License
2.11k stars 440 forks source link

limited column count? #48

Closed jasonnet closed 7 years ago

jasonnet commented 7 years ago

I seem to have trouble compiling this code if the number of columns is greater than 10. For example here is code with 11 columns specified.

Invoking.. $ gcc -g -Wall -std=c++11 -I . -c test1.cpp where test1.cpp is...

#include "src/include/fast-cpp-csv-parser/csv.h"

int main(int argc, char* argv[] ) {
  io::CSVReader<11> * in = new io::CSVReader<11>("testdata.csv");
  in->set_header("a","b","c","d","e","f","g","h","i","j",'k');//,"l");

  typedef char * valtype;
  valtype sa=0,sb=0,sc=0,sd=0,se=0,sf=0,sg=0,sh=0,si=0,sj=0,sk=0;//,sl=0;

  in->read_row(sa,sb,sc,sd,se,sf,sg,sh,si,sj,sk);//,sl);
}

The error message include:


./src/include/fast-cpp-csv-parser/csv.h:1104:41: error: no matching function for call to ‘io::CSVReader<11u>::set_column_names(char)’
                         set_column_names(std::forward<ColNames>(cols)...);
                                         ^

The 3,6,9,10 column versions compile fine. I've tried this with gcc ver 4.9.2 and ver 5.4.0

ben-strasser commented 7 years ago

Hi,

thanks for the report.

The reason why your code does not work is a very subtle bug in your code. Your last column header is a char 'k' and not a char array "k" as the remaining parameters. This is a problem because std::string is not constructible from 'k' but only from "k".

If you need to be able to specify chars as header names, then add

             template<class ...ColNames>
             void set_column_names(char c, ColNames...cols){

column_names[column_count-sizeof...(ColNames)-1] = std::string(1, c); set_column_names(std::forward(cols)...); }

in line 1106 of the header. Do you have any good usecase for this? I prefer to keep the interface as narrow as possible.

However, I'm wondering whether adding a

             template<class ...ColNames>
             void set_column_names(char c, ColNames...cols){
                     static_assert(false, "You passed a character 

literal where a string literal is expected. You probably put a ' where a \" is expected.") }

is a good idea.

Best Regards Ben Strasser

On 07/12/2017 06:17 AM, jasonnet wrote:

I seem to have trouble compiling this code if the number of columns is greater than 10. For example here is code with 11 columns specified.

Invoking.. |$ gcc -g -Wall -std=c++11 -I . -c test1.cpp| where test1.cpp is...

|#include "src/include/fast-cpp-csv-parser/csv.h" int main(int argc, char argv[] ) { io::CSVReader<11> in = new io::CSVReader<11>("testdata.csv"); in->set_header("a","b","c","d","e","f","g","h","i","j",'k');//,"l"); typedef char * valtype; valtype sa=0,sb=0,sc=0,sd=0,se=0,sf=0,sg=0,sh=0,si=0,sj=0,sk=0;//,sl=0; in->read_row(sa,sb,sc,sd,se,sf,sg,sh,si,sj,sk);//,sl); } |

The error message include:

|./src/include/fast-cpp-csv-parser/csv.h:1104:41: error: no matching function for call to ‘io::CSVReader<11u>::set_column_names(char)’ set_column_names(std::forward(cols)...); ^ |

The 3,6,9,10 column versions compile fine. I've tried this with gcc ver 4.9.2 and ver 5.4.0

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ben-strasser/fast-cpp-csv-parser/issues/48, or mute the thread https://github.com/notifications/unsubscribe-auth/ALaAj8xKLwWRHi91rurCeTYS-sx8WMjjks5sNEj2gaJpZM4OVG56.

ben-strasser commented 7 years ago

The static_assert is not a good idea, as it does not play nice with implicit conversions.