Format specifier for templated type

梦想与她 提交于 2019-12-24 13:33:43

问题


I've got a C++ class that's templated on the integer type, e.g.,

template<typename int_type>

Say that somewhere in that class, I'd like to use sscanf for reading some values from a file, e.g.,

int_type num_rows;
fgets( buffer, BUFSIZE, in_file );
sscanf( buffer, "%d", &num_rows);

The format specifier only works correctly if the int_type is the intrinsic int.

Is there a better way of treating the format specifier for general int_type?


回答1:


Instead of using sscanf() and a format specifier use std::istringstream with operator>>():

if (fgets( buffer, BUFSIZE, in_file ))
{
    std::istringstream in(buffer);
    if (!(in >> num_rows))
    {
        // Handle failure.
    }
}

Replacing the (not shown) FILE* and with a std::ifstream would enable removal of the std::istringstream and just read directly from the std::ifstream instead.




回答2:


You can just declare fmt in your class and provide explicit values per type in the implementation:

// foo.hpp
template< typename T >
class foo
{
private:
    static const char* fmt;

public:
    void print() const
    {
        T num_rows;
        fgets( buffer, BUFSIZE, in_file );
        sscanf( buffer, fmt, &num_rows);
    }
};

and

// foo.cpp
template<> const char* foo< int >::fmt = "%d";
template<> const char* foo< long >::fmt = "%ld";



回答3:


You could create template helper class:

template<class int_type>
struct fmt_helper_f{};

template<>
struct fmt_helper_f<int>
{
  static const char * fmt() {return "%d";}
};

template<>
struct fmt_helper_f<float>
{
  static const char * fmt() {return "%f";}
};

Actually you may want to use streams for input and output



来源:https://stackoverflow.com/questions/15929326/format-specifier-for-templated-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!