如何检测我何时向可变参数函数传递"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()
,到我的可变参数函数时,如果我在我的Sublime Text构建上运行shell script
if,我的程序就会运行。这就是正在发生的事情,如果我通过一个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::string
。typeid( temp ) == typeid( std::string* )
让它过去有什么错?
无论如何,另一种可能的解决方案可能是接受std::string
而不是c_str()
,即std::string s.c_str()
。
我找到了使用可变参数模板的第三部分库(include),解决了这个问题。
- 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]
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 使用一个考虑到std::map中键值的滚动或换行的键
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么 std::unique 不调用 std::sort?
- 使用std::函数映射对象方法
- 如何检测我何时向可变参数函数传递"std::string"而不是"c_str()"