简体   繁体   English

如何在 c++17 中使用嵌套的 static 模板结构

[英]How to use Nested static templated structs in c++17

What I am trying to achieve is to use a struct as a template parameter for another struct, without instantiation, so that it all happens at compile time.我想要实现的是使用一个结构作为另一个结构的模板参数,而不需要实例化,这样这一切都发生在编译时。 For example:例如:

template<int v>
struct Container {
    static const int value = v;
};

template<Container a, Container b>
struct BiggerContainer {
    static const int value = a.v > b.v ? a.v : b.v;
};

int main() {
std::cout << BiggerContainer<Container<42>, Container<42>>::value << std::endl;

return 0;
}

The error I get with this code is the following:我使用此代码得到的错误如下:

./code.cpp: In function 'int main()':
./code.cpp:44:61: error: type/value mismatch at argument 1 in template parameter list for 'template<Container<...auto...> a, Container<...auto...> b> struct BiggerContainer'
   44 |     std::cout << BiggerContainer<Container<42>, Container<42>>::value << std::endl;
      |                                                             ^~
./code.cpp:44:61: note:   expected a constant of type 'Container<...auto...>', got 'Container<42>'
./code.cpp:44:61: error: type/value mismatch at argument 2 in template parameter list for 'template<Container<...auto...> a, Container<...auto...> b> struct BiggerContainer'
./code.cpp:44:61: note:   expected a constant of type 'Container<...auto...>', got 'Container<42>'

What am I getting wrong here?我在这里做错了什么? What exactly does this error mean what could I change to get at what I'm trying?这个错误到底是什么意思我可以改变什么来得到我正在尝试的东西?

Thank you!谢谢!

You might have:你可能有:

template <int v>
struct Container {
    static const int value = v;
};
// template <int v> using Container = std::integral_constant<int, v>;

template <typename lhs, typename rhs>
struct Max
{
    static const int value = lhs::value > rhs::value ? lhs::value : rhs::value;
};

int main() {
    std::cout << Max<Container<42>, Container<42>>::value << std::endl;
}

In C++17 you cannot use a class or struct as a non-type template parameter.在 C++17 中,您不能使用 class 或 struct 作为非类型模板参数。 Non-type parameters have to be ints, pointers, enums, or references.非类型参数必须是整数、指针、枚举或引用。 This changes in C++20.这在 C++20 中发生了变化。

Specifically, the following declaration parameterizes BiggerContainer on values of structs, which is not possible.具体来说,以下声明将BiggerContainer参数化为结构的,这是不可能的。

template<Container a, Container b>
struct BiggerContainer

There are a few other problems with the code, but this is the main issue which will cause you to have to redesign things.代码还有一些其他问题,但这是导致您不得不重新设计的主要问题。

The problem is that in your template arguments for BiggerContainer , you need to specify the template arguments for Container ( template<Container, Container> doesn't have template arguments for Container ).问题是在 BiggerContainer 的模板BiggerContainer中,您需要为Container指定模板 arguments ( template<Container, Container>没有用于Container的模板 arguments )。

One alternative is to use a template function, like this:一种替代方法是使用模板 function,如下所示:

template<int v1, int v2>
int BiggerContainer(Container<v1> a, Container<v2> b)
{
    return a.value > b.value ? a.value : b.value;
}

Then calling it like:然后像这样调用它:

BiggerContainer(Container<42>(), Container<42>()); // No need to add '::value'

NOTE: Must call constructer, Container<val>() not just a type Container<val> .注意:必须调用构造函数Container<val>()而不仅仅是Container<val>类型。 (Because we're passing a value, not a type) (因为我们传递的是一个值,而不是一个类型)

The problem with that solution is that it returns an int (value type), not a Container , but it may be okay for some uses.该解决方案的问题在于它返回一个 int (值类型),而不是Container ,但对于某些用途来说可能没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM