3

问题:

例如,如果你想要一个可变参数函数,它接受任意数量的参数Args&&...args,并打印所有这些参数t时间。更重要的是,您希望t默认为1,因此默认打印args一次。

你会尝试的第一件事是:

template <typename ... Args>
void foo(Args... args, unsigned t = 1) {
    for (unsigned i = 0; i < t; ++i) {
        (std::cout << ... << args);
    }
}

显然,除非您显式传入模板参数,否则这不起作用:

// Error: expected 1 argument, got 2
foo(0, "Hello, world!");

因为模板推导时默认参数被视为普通参数,参数包始终为空。这会阻止您使该功能有用。(相关问题

然后我决定使用聚合初始化(尤其是从 c++ 20 开始的指定初始化程序)来模拟更强大的“默认参数”。它看起来像这样:

struct foo_t {
    unsigned t = 1;
    template <typename ... Args>
    void operator() (Args... args) {
        for (unsigned i = 0; i < t; ++i) {
            (std::cout << ... << args);
        }
    }
};

int main() {
    foo_t{}("Hello, ", "World!\n");      // prints 1 line
    foo_t{ 5 }(0, "Hello, world!\n");    // prints 5 lines
    return 0;
}

此外,借助 c++ 20 指定的初始化程序,这可以解决人们无法“跳过”默认函数参数的抱怨:

struct bar_t {
    const std::string& str = "Hello, world!";
    int t = 1;
    void operator() () {
        for (int i = 0; i < t; ++i) {
            std::cout << str << std::endl;
        }
    }
};

int main() {
    // Skips .str, using the default "Hello, World!"
    bar_t{ .t = 10 }();
    return 0;
}

我想知道这样做是否有任何潜在的陷阱。

背景(可以安全地忽略)

所以昨天我在SO附近闲逛,遇到一个问题(但后来被删除)询问如何将默认std::source_location参数与可变参数模板结合起来:

template<typename... Args>
void log(Args&&... args, const std::experimental::source_location& location = std::experimental::source_location::current()) {
    std::cout << location.line() << std::endl;
}

显然,正如问题中所述,这不能按预期工作。所以我想出了以下代码:

struct logger {
    const std::experimental::source_location& location = std::experimental::source_location::current();
    template <typename... Args>
    void operator() (Args&&... args) {
        std::cout << location.line() << std::endl;
    }
};

int main(int argc, char** argv) {
    logger{}("I passed", argc, "arguments.");
    return 0;
}

但发现它可以做更多,因此这个问题。

4

1 回答 1

2

生命周期(扩展)至少有一个陷阱:

const std::string& str = "Hello, world!";创建悬空指针,(成员没有生命周期延长)。

以下很好:

void repeat_print(const std::string& str = "Hello, world!", int t = 1) {/*..*/}

int main()
{
    repeat_print();
}

但以下不是:

struct bar_t {
    const std::string& str = "Hello, world!";
    int t = 1;
    void operator() () const { /*..*/ }
};

int main()
{
    bar_t{}();
}

您可能会修复bar_t按值获取成员,但在某些情况下您会做额外的复制。

于 2019-10-17T09:01:49.817 回答