【发布时间】:2021-02-10 19:41:56
【问题描述】:
我认为下面的代码是不言自明的。我可以轻松地将静态变量传递给模板参数,它可以按预期工作。使用静态数组将清理代码,因此看起来更好,但不幸的是,由于我在评论中粘贴的错误,它无法编译。请注意,它是由带有 c++17 标志的 gcc 10.2 编译的。 所以问题是如何将数组元素传递给模板。
#include <iostream>
#include <vector>
#include <tuple>
using DataTransfer = std::tuple<char, int>;
using DataPool = std::vector<DataTransfer>;
typedef struct Event
{
DataPool dataPool;
const char* description;
} Event;
template <Event& event>
class EventTransmitter
{
public:
EventTransmitter()
{
std::cout<<event.description<<"\n";
}
};
static Event ev1{ {{'d', 4}, {'a', 1}}, "Description 1"};
static Event ev2{ {{'g', 7}, {'b', 6}}, "Description 2"};
static Event evs[2] {
{ {{'d', 4}, {'a', 1}}, "Description 1"},
{ {{'g', 7}, {'b', 6}}, "Description 2"}
};
int main()
{
EventTransmitter<ev1> e1;
EventTransmitter<ev2> e2;
//EventTransmitter<evs[0]> e3;
//error: '& evs[0]' is not a valid template argument of
//type 'Event&' because 'evs[0]' is not a variable
return 0;
}
【问题讨论】:
-
为什么要从模板中为每个对象创建一个新类?我不知道这是可能的。为什么不直接将对象传递给构造函数?你甚至不需要模板。始终以最不令人惊讶的方式编写程序,您的代码非常令人惊讶。
-
gcc (trunk) 使用
-std=c++20编译代码并给出预期的输出,但不是-std=c++17。 clang 可能给出了更好的错误信息:non-type template argument refers to subobject 'evs[0]'godbolt.org/z/6exajh -
@mch 假设您的文档需要只能处理特定事件的事件处理程序。将错误的处理程序绑定到错误的事件时,每个处理程序都有不同的类型允许编译错误。这只是您想要使用此类代码的一种可能性。
-
@mch 这只是非常简化的代码,因此它可读。实际上,还有很多事情迫使我像上面的代码那样做。
标签: c++ arrays templates c++17