如何检测我何时将 `std::string` 而不是 `c_str()` 传递给我的可变参数函数?

How to detect when I am passing a `std::string`, instead of a `c_str()` to my variadic function?

如何检测我何时将 std::string 而不是 c_str() 传递给我的可变参数函数?

我最初的问题是,当我向可变参数函数传递 std::string 而不是 c_str(),即 std::string s.c_str() 时,如果我 运行 如果来自 shell script 我的 Sublime Text 版本。这就是正在发生的事情,如果我通过 std::string 并尝试 运行 如果来自 Sublime Text:

rm -f main.exe
g++ --std=c++11 main.cpp -I . -o main
Starting the main program...

[Finished in 3.4s]

但是如果我从 shell 和 ./main 中 运行,一切正常,除了字符串 print:

Starting the main program...

argumentsCount: 3
argumentsStringList[0]: ./main
argumentsStringList[1]: 1
argumentsStringList[2]: 2

SourceCode::SourceCode(1) text_code: ▒
Exiting main(2)

这是我进行解析的函数:

/**
 * Missing string printf. This is safe and convenient but not exactly efficient.
 *
 * @param fmt     a char array
 * @param ...     a variable length number of formating characters.
 *
 * @see 
 * @see 
 */
inline std::string format(const char* fmt, ...)
{
    int   size   = 512;
    char* buffer = new char[size];

    va_list var_args;
    va_start(var_args, fmt);

    int nsize = vsnprintf(buffer, size, fmt, var_args);

    //fail delete buffer and try again
    if(size<=nsize)
    {
        delete[] buffer;

        buffer = 0;
        buffer = new char[nsize+1]; //+1 for /0
        nsize  = vsnprintf(buffer, size, fmt, var_args);
    }

    std::string ret(buffer);
    va_end(var_args);

    delete[] buffer;
    return ret;
}

这是我的暂定。这是一个最小的实际例子,你可以 运行 在你自己的电脑上编译 g++ --std=c++11 main.cpp -o main:

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstdarg>
#include <typeinfo>

inline std::string format(const char* fmt, ...)
{
    int   size   = 512;
    char* buffer = new char[size];

    std::string* temp;
    va_list var_args_copy;

    printf( "%s\n", fmt );
    va_start(var_args_copy, fmt);

    do
    {
        temp = va_arg( var_args_copy, std::string* );

        if( typeid( temp ) == typeid( std::string* ) )
        {
            std::string message( "ERROR!!!!!!!!!!!!!! Bad formatted string!\n" );
            return message;
        }

    } while( temp != NULL );

    va_list var_args;
    va_start(var_args, fmt);

    int nsize = vsnprintf(buffer, size, fmt, var_args);

    //fail delete buffer and try again
    if(size<=nsize)
    {
        delete[] buffer;

        buffer = 0;
        buffer = new char[nsize+1]; //+1 for /0
        nsize  = vsnprintf(buffer, size, fmt, var_args);
    }

    std::string ret(buffer);
    va_end(var_args);

    delete[] buffer;
    return ret;
}

int main( int argumentsCount, char* argumentsStringList[] )
{
    printf( "" );

    std::string strin( "String" );

    std::cout << format( "1. The string is %s", strin ) << std::endl;
    std::cout << format( "2. The string is %s", strin.c_str() ) << std::endl;

    printf( "Exiting main(2)" );
    return EXIT_SUCCESS;
}

运行 我的暂定,它输出我这个:

g++ -std=c++11 main2.cpp -I . -o main
1. The string is %s
ERROR!!!!!!!!!!!!!! Bad formatted string!

2. The string is %s
ERROR!!!!!!!!!!!!!! Bad formatted string!

Exiting main(2)[Finished in 3.7s]

这里的2. The string is %sERROR!!!!!!!!!!!!!! Bad formatted string!不能打印,因为它不是std::stringtypeid( temp ) == typeid( std::string* ) 让它过去有什么问题?

无论如何,另一种可能的解决方案是只接受 std::string 而不是 c_str(),即 std::string s.c_str().

我找到了第三方库(include),它使用可变参数模板,解决了这个问题。

  1. https://github.com/c42f/tinyformat

现在是代码:

#include <cstdlib>
#include "libraries/tinyformat/tinyformat.h"

int main( int argumentsCount, char* argumentsStringList[] )
{
    printf( "" );

    std::string strin( "String" );

    std::cout << tfm::format( "1. The string is %s", strin ) << std::endl;
    std::cout << tfm::format( "2. The string is %s", strin.c_str() ) << std::endl;

    printf( "Exiting main(2)\n" );
    return EXIT_SUCCESS;
}

刚刚正确输出:

rm -f main.exe
g++ -std=c++11 main2.cpp -I . -o main
1. The string is String
2. The string is String
Exiting main(2)
[Finished in 4.3s]