本文介绍了C ++ 20非类型模板参数,它是先前类型参数中的模板:不是有效模板arg,因为不是变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我已经为此感到困惑了一段时间,并且提炼了一个简单的错误消息示例.有什么办法可以使这项工作吗?我只是想念一个模板"消息吗?或类型名称"某个地方?

I've been stumped on this for a while, and distilled a simple example of my error message. Is there some way to make this work? Am I just missing a "template" or "typename" somewhere?

#include <cstdint>

template<typename T, typename J>
struct silly
{
    const void (*f)(T,J);
};

template<typename T, typename J, silly<T, J> aSilly>
struct sillier
{
    const uint32_t something;
};

void dumb_func(uint32_t i, uint32_t j)
{
    return;
}

constexpr silly<uint32_t, uint32_t> mySilly{ .f = dumb_func };

using silliest = sillier<uint32_t, uint32_t, mySilly>;

int main()
{
    return 2;
}

g ++吐出来:

g++ -std=c++2a ugh.cpp

ugh.cpp:20:51: error: invalid conversion from ‘void (*)(uint32_t, uint32_t)’ {aka ‘void (*)(unsigned int, unsigned int)’} to ‘const void (*)(unsigned int, unsigned int)’ [-fpermissive]
   20 | constexpr silly<uint32_t, uint32_t> mySilly{ .f = dumb_func };
      |                                                   ^~~~~~~~~
      |                                                   |
      |                                                   void (*)(uint32_t, uint32_t) {aka void (*)(unsigned int, unsigned int)}
ugh.cpp:20:51: error: ‘dumb_func’ is not a valid template argument of type ‘const void (*)(unsigned int, unsigned int)’ because ‘dumb_func’ is not a variable

我尝试阅读 https://en.cppreference.com/w/cpp/language/template_parameters ,但是我在这里超出了模板的深度.实际的用例是T和J类似于std :: array< float,?>.然后再使sillier T,J,silil T,J>改变.

I've tried reading through https://en.cppreference.com/w/cpp/language/template_parameters but I'm out of my template depth here. The actual use case for this has T and J being something like std::array<float, ?> and then sillier<T, J, silly<T, J>> inherits from some other more general class, and has overrides which call functions like f so that everything gets inlined into specialized functions for a given instance of silly<T, J>.

推荐答案

这种解释没有任何意义,实际上代码可能还不错,并且错误可能只是 answer 中发现并报告的97700"rel =" nofollow noreferrer>错误.不过,下面给出的修复程序仍然有效.

This explanation doesn't make sense, and in fact the code is probably fine, and the error might just be a bug as discovered and reported in this answer. The fix given below still works though.

您可以将第三个模板参数设为引用类型的非类型模板参数.

You can make the third template parameter be a non-type template parameter of reference type.

template<typename T, typename J, silly<T, J> const & aSilly>
                                          // ^^^^^^^
struct sillier
{
    const uint32_t something;
};


此外, silly 中的成员变量 f 被声明为返回 const void 类型的函数指针:


Also, the member variable f in silly is declared as a function pointer returning a const void type:

const void (*f)(T,J);

因此,您需要从 f 的返回类型中删除 const ,或者可以更改 dumb_func 的声明以返回 const void 类型:

So you either need to remove the const from the return type of f, or you can change the declaration of dumb_func to return a const void type:

const void dumb_func(uint32_t i, uint32_t j)
{
    return;
}

const void 返回函数中似乎没有任何意义,所以我会选择第一个选项.

There doesn't seem to be any point in a const void returning function, so I would go with the first option.

这是一个演示.

这篇关于C ++ 20非类型模板参数,它是先前类型参数中的模板:不是有效模板arg,因为不是变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:08