我有一个类,它包含一个"错误"函数,可以格式化一些文本.我想接受可变数量的参数,然后使用printf格式化它们.
例:
class MyClass
{
public:
void Error(const char* format, ...);
};
Run Code Online (Sandbox Code Playgroud)
Error方法应该接受参数,调用printf/sprintf来格式化它然后用它做一些事情.我不想自己编写所有格式,因此尝试找出如何使用现有格式是有意义的.
这似乎是一个愚蠢的问题,但当我试图在SOF中查看这个答案时,
我注意到这样的陈述:
template<
typename IntType, std::size_t Cols,
IntType(*Step)(IntType),IntType Start, std::size_t ...Rs
>
constexpr auto make_integer_matrix(std::index_sequence<Rs...>)
{
return std::array<std::array<IntType,Cols>,sizeof...(Rs)>
{{make_integer_array<IntType,Step,Start + (Rs * Cols),Cols>()...}};
}
Run Code Online (Sandbox Code Playgroud)
进一步来说 :
std::size_t ...Rs
Run Code Online (Sandbox Code Playgroud)
要么
std::index_sequence<Rs...>
Run Code Online (Sandbox Code Playgroud)
什么......意味着什么?
报告为与此问题相关的原始问题的问题不正确:
那个问题不能回答这两种情况(因为它们不是具有可变数量参数的函数)
std::size_t ...Rs
std::index_sequence<Rs...>
Run Code Online (Sandbox Code Playgroud)
但这是一个很好的解释:
https://xenakios.wordpress.com/2014/01/16/c11-the-three-dots-that-is-variadic-templates-part/
考虑以下程序:
#include <iostream>
struct Test
{
int a;
Test() : a(3)
{ }
Test(const Test& t...)
{
std::cout<<"Copy constructor called\n";
a=t.a;
}
int get_a()
{
return a;
}
~Test()
{
std::cout<<"Destructor is called\n";
}
};
int main()
{
Test t;
Test* t1=new Test(t);
std::cout<<t.get_a()<<'\n';
std::cout<<t1->get_a()<<'\n';
delete t1;
}
Run Code Online (Sandbox Code Playgroud)
仔细观察复制构造函数参数中的三个点我尝试这个程序时真的很惊讶.有什么用?这是什么意思?
语言规范对此有何看法?
我知道三个点用于表示变量函数(如等)中的变长参数printf()以及scanf()C99引入的可变参数宏.在C++中,如果我没有错,它们将用于可变参数模板.
这段代码是否形成良好?这个可变参数构造函数是否可以使用任意数量的参数?
它在g ++ 4.8.1和MSVS 2010上编译并运行良好.
我正在尝试检查一个类是否有方法operator==.我在这里找到了一个SFINAE的解决方案,它与我制作的课程一起工作正常.
它看起来像这样:
template <typename T>
class comparable
{
typedef char one;
typedef long two;
template <typename C> static one test( typeof(&C::operator==) ) ;
template <typename C> static two test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试:
std::cout << comparable<int>::value << std::endl;
Run Code Online (Sandbox Code Playgroud)
然后它返回false,而我期望它返回true.为什么是这样 ?
我如何创建一个带有可变数量参数的宏,并使用std :: cout打印出来?很抱歉,如果这是一个菜鸟问题,在搜索答案后找不到任何澄清可变参数的宏.
概念示例:
#include <iostream>
#define LOG(...) std::cout << ... << ... << std::endl
int main() {
LOG("example","output","filler","text");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
exampleoutputfillertext
Run Code Online (Sandbox Code Playgroud) 我正在阅读有关严格别名的内容,但它仍然有点模糊,我无法确定定义/未定义行为的界限.我发现最详细的帖子集中在C.所以如果你能告诉我这是否允许以及自C++ 98/11以来发生了什么变化,那将是很好的...
#include <iostream>
#include <cstring>
template <typename T> T transform(T t);
struct my_buffer {
char data[128];
unsigned pos;
my_buffer() : pos(0) {}
void rewind() { pos = 0; }
template <typename T> void push_via_pointer_cast(const T& t) {
*reinterpret_cast<T*>(&data[pos]) = transform(t);
pos += sizeof(T);
}
template <typename T> void pop_via_pointer_cast(T& t) {
t = transform( *reinterpret_cast<T*>(&data[pos]) );
pos += sizeof(T);
}
};
// actually do some real transformation here (and actually also needs an inverse)
// ie this …Run Code Online (Sandbox Code Playgroud) 最近我找到了带有三个点参数的函数原型.我编写了自己的函数并编译得很好:
void func(int a, ...){}
Run Code Online (Sandbox Code Playgroud)
那是什么意思?
更新
感谢你们!我想到了.这是我的例子:
void func(unsigned int n_args, int arg, ...)
{
for(unsigned int i = 0; i < n_args; ++i)
cout << *((int*)&arg + i) << ' ';
}
Run Code Online (Sandbox Code Playgroud)
此函数打印出由空格字符分隔的参数.
lambda的以下用法是错误的,脆弱的还是愚蠢的?它适用于VC++ 2012,但我担心有一些变量 - 参数/ lambda堆栈交互使得这很危险.
class
ArgumentException : public std::runtime_error
{
public:
ArgumentException(
const char* format_,
... )
: std::runtime_error(
[&]()
{
char buffer[2048];
va_list arguments;
va_start ( arguments, format_ );
int writtenCount = vsnprintf( buffer, 2048, format_, arguments );
va_end ( arguments );
return std::string(buffer);
}() )
{
}
};
Run Code Online (Sandbox Code Playgroud) 我可以重载我的函数来使用JavaScript中的许多参数做一些事情.例如:
function f()
{
alert(arguments[0]);
}
f(4); // will alert 4
Run Code Online (Sandbox Code Playgroud)
我可以用C++做同样的事情吗?
为什么下面的代码总是打印"type is double"?(我在StackOverflow中看到过这段代码)
#include <iostream>
void show_type(...) {
std::cout << "type is not double\n";
}
void show_type(double value) {
std::cout << "type is double\n";
}
int main() {
int x = 10;
double y = 10.3;
show_type(x);
show_type(10);
show_type(10.3);
show_type(y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)