Default values for array arguments

前端 未结 6 792
孤独总比滥情好
孤独总比滥情好 2021-01-04 06:53

Just playing around a little with C++. What I really want to do is to be able to setup a function with default values defined for an array or pointer argument. To keep thing

6条回答
  •  抹茶落季
    2021-01-04 07:38

    One simple way to do this is via plain old function overloading. For example, below simulates the default parameter value for format parameter which is char* type:

    static string to_string(time_point time)
    {
        return to_string(time, "%Y-%m-%d-%H-%M-%S");
    }
    
    static string to_string(time_point time, const char* format)
    {
        time_t tt = system_clock::to_time_t(time);
    
        char str[1024];
        if (std::strftime(str, sizeof(str), format, std::localtime(&tt)))
            return string(str);
        else return string();
    }
    

提交回复
热议问题