小编Ada*_*enz的帖子

如何将传递给variadic宏的可变参数求和?

我想要一个程序来定义一个宏,它可以计算参数的数量,并将它们传递给一个函数sum,该函数对参数的值进行求和并返回总数.我设法在GCC上做了,但我想在Visual C++ 14上实现它.

#include "stdafx.h"
#include <iostream>
#include <cstdarg>


#define ELEVENTH_ARGUMENT(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, ...) a11
#define COUNT_ARGUMENTS(...) ELEVENTH_ARGUMENT(dummy, ## __VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)

#define SUM(...) sum(ELEVENTH_ARGUMENT(dummy, ## __VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))


int sum(int n, ...) {

    int sz{ n };
    va_list ap;
    va_start(ap, n);
    int tmp{};
    while (--sz)
        tmp += va_arg(ap, int);

    va_end(ap);

    return tmp;
} …
Run Code Online (Sandbox Code Playgroud)

c++

10
推荐指数
2
解决办法
496
查看次数

在算术表达式中会将double隐式转换为unsigned int吗?

在Stanley B. Lippman的C ++ Primer中,“隐式转换”部分指出:

  int ival; 
  unsigned int ui;
  float fval;

  fval = ui - ival * 1.0;
Run Code Online (Sandbox Code Playgroud)

ival转换为double,然后乘以1.0。结果转换为unsigned int,然后减去ui。结果转换为float,然后分配给fval

但是我不这么认为:我认为实际上ival是将类型转换为double然后乘以1.0then ui,将类型unsigned int转换为double的情况并非相反,然后将乘法结果从转换为double的ui值中减去。最后将此最终double值转换为float并将其分配给fval

为了确保我说的话:

ival = 5; 
ui  = 10;
fval = 7.22f;
dval = 3.14;

std::cout << typeid(ui - ival * 1.0).name() << std::endl; // double

std::cout << (ui - ival * 1.7) << …
Run Code Online (Sandbox Code Playgroud)

c++ type-conversion

9
推荐指数
1
解决办法
136
查看次数

为什么不将临时对象传递给对象初始化调用复制constrctor?

通过直接初始化传递临时对象来初始化对象时我有些困惑.

这是我想要了解的代码:

class Foo {
    public:
        Foo() { cout << "ctor()\n"; }
        Foo(int) { cout << "ctor(int)\n"; }
        Foo(const Foo&) { cout << "cpy-ctor\n"; }
        Foo& operator=(const Foo&) { cout << "copy-assignment\n"; return *this; }
};


int main() {

    Foo();// ctor (temporary object). it is not a function prototype because it requires a return type.
    cout << endl;

    Foo f; // ctor()
    cout << endl;

    Foo f2(Foo(7)); // ctor only once
    cout << endl;

    Foo f3(Foo(Foo(7))); // ctor(int), cpy-ctor() only …
Run Code Online (Sandbox Code Playgroud)

c++ oop constructor initialization

6
推荐指数
0
解决办法
83
查看次数

我可以使用auto或decltype代替尾随返回类型吗?

我发现trailing return type定义返回复杂类型的函数的返回非常容易,例如:

auto get_diag(int(&ar)[3][3])->int(&)[3]{ // using trailing return type
    static int diag[3]{
        ar[0][0], ar[1][1], ar[2][2]
    };
    return diag;
}

auto& get_diag2(int(&ar)[3][3]){ // adding & auto because otherwise it converts the array to pointer
    static int diag[3]{
        ar[0][0], ar[1][1], ar[2][2]
    };
    return diag;
}

int main(){

    int a[][3]{
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    decltype(get_diag(a)) diag{
        get_diag(a)
    };

    for (auto i : diag)
        std::cout << i << ", ";
    std::cout << std::endl;

    decltype(get_diag2(a)) diag2{ …
Run Code Online (Sandbox Code Playgroud)

c++ auto c++11 trailing-return-type

5
推荐指数
1
解决办法
223
查看次数

为什么我不能/不能在类中重新定义类型名?

据说a type name使用后不能在类内部定义。例如:

typedef double Money;
class Account {
public:
    Money balance() { return bal; } // uses Money from the outer
private:
    typedef double Money; // error: cannot redefine Money
    Money bal;
    // ...
};
Run Code Online (Sandbox Code Playgroud)

该程序在GCC上不起作用,但在MSVC 14上却可以正常工作!*我从“ C ++入门手册第5版”中获得了此示例。

c++

5
推荐指数
0
解决办法
36
查看次数