C++ 如何检测何时将'std::string'而不是'c_str()'传递给变量函数? 如何检测何时将std::string而不是c_str()传递给变量函数?
我最初的问题是,当我将C++ 如何检测何时将'std::string'而不是'c_str()'传递给变量函数? 如何检测何时将std::string而不是c_str()传递给变量函数?,c++,string,c++11,stl,std,C++,String,C++11,Stl,Std,我最初的问题是,当我将std::string而不是c_str()即std::string s.c_str()传递给我的变量函数时,如果我在升华文本构建上运行if fromshell脚本,我的程序就会运行。这就是发生的情况,如果我传递一个std::string,并尝试从升华文本运行if: rm -f main.exe g++ --std=c++11 main.cpp -I . -o main Starting the main program... [Finished in 3.4s] 但是,
std::string
而不是c_str()
即std::string s.c_str()
传递给我的变量函数时,如果我在升华文本构建上运行if fromshell脚本,我的程序就会运行。这就是发生的情况,如果我传递一个std::string
,并尝试从升华文本运行if:
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 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上的。字符串是%s
,错误!!!!!!!!!!!!!!格式错误的字符串代码>不能打印,因为它不是std::string
。让它过去的typeid(temp)==typeid(std::string*)
有什么不对
无论如何,另一种可能的解决方案是只接受std::string
而不是c_str()
,即std::string s.c_str()
我找到了第三部分库(include),它使用可变模板,解决了这个问题
下面是代码:
#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;
}
这就是C变量的特点。它们很不安全。如果您想要类型安全,请使用可变模板。您不能使用粗糙的C型变量。没有任何关于某个随机内存块的东西会说“我是一个std::string
,我是一个c_str()”。如果你想正确地使用这个,请使用适当的C++模板。
#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]