参数包后具有非推导类型的参数 [英] Parameter with non-deduced type after parameter pack

查看:27
本文介绍了参数包后具有非推导类型的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于下一个程序,clang++g++ 有不同的行为:

There is different behaviour in clang++ and g++ for the next program:

#include <type_traits>
#include <utility>

template< std::size_t index, typename type >
struct ref { type & value; };

template< std::size_t index, typename type >
type && get(ref< index, type > const & r)
{
    return std::forward< type >(r.value);
}

template< typename F, typename ...types, std::size_t ...indices >
decltype(auto) apply_inverse(F & f, types &... values, std::index_sequence< indices... >)
{
    struct : ref< indices, types >... {} refs{{values}...};
    constexpr std::size_t top = sizeof...(indices) - 1;
    return std::forward< F >(f)(get< top - indices >(refs)...);
}

template< typename F, typename ...types >
decltype(auto) apply_inverse(F && f, types &&... values)
{
    return apply_inverse< F, types... >(f, values..., std::index_sequence_for< types... >{});
}

#include <iostream>

int main()
{
    auto const print = [] (auto const &... value) -> std::ostream & { return (std::cout << ... << value); };
    apply_inverse(print, 1, 2, 3) << std::endl;
}

实例.

它只是尝试恢复传递的参数并对它们应用一些函数.

It just tries to revert the arguments passed and applies some function to them.

对于 G++ 它编译得很好,但是对于 clang++(甚至来自主干)它给出了以下错误:

For G++ it compiles fine, but for clang++ (even from trunk) it gives the following error:

错误:没有匹配的函数调用'apply_inverse'

error: no matching function for call to 'apply_inverse'

我认为原因是,在上重载中,函数原型中的参数包后有一个参数.但是参数包中所有参数的类型都是明确指定的.

I think the reason is the fact, that in upper overloading there is a parameter after parameter pack in the function prototype. But types for all the arguments in arguments pack are explicitly specified.

编译器接受代码是否正确?

Is it right for compiler to accept the code?

推荐答案

没有具体说明是哪个版本的 Clang 拒绝了上面的代码.

It was not specified exactly what version of Clang refused the code above.

但此时 Clang 12 接受了它,以及 GCC 和 MSVC:https://gcc.godbolt.org/z/qMc9fKTEf

But at this moment Clang 12 accepts it, as well as GCC and MSVC: https://gcc.godbolt.org/z/qMc9fKTEf

所以代码是完全合法的.

So the code is perfectly legal.

这篇关于参数包后具有非推导类型的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆