如何检测我何时向可变参数函数传递"std::string"而不是"c_str()"

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

本文关键字:quot std string str 何时 检测 何检测 函数 参数 变参      更新时间:2023-10-16

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

我最初的问题是,当我确实传递了一个std::string,而不是一个c_str(),即std::string s.c_str(),到我的可变参数函数时,如果我在我的Sublime Text构建上运行shell scriptif,我的程序就会运行。这就是正在发生的事情,如果我通过一个std::string并尝试从崇高文本运行:

rm -f main.exe
g++ --std=c++11 main.cpp -I . -o main
Starting the main program...
[Finished in 3.4s]

但是如果我从带有./main的 shell 运行,一切都很好,除了字符串打印:

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 http://stackoverflow.com/a/10150393/4934640
* @see http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf/10150393#10150393
*/
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( "%sn", 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 %s上,ERROR!!!!!!!!!!!!!! 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]