gpt4 book ai didi

c++ - 什么模式匹配(如果有)适用于 C++ 可变参数模板函数调用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:35 24 4
gpt4 key购买 nike

我只是天真地写了这个:

#include <stdio.h>

template< class... Args >
auto format_for( Args... args, int last_arg )
-> char const*
{
// using Specifier = char const [3];
// static Specifier const s[] = { {'%', 'f'+(0*args), ' '}..., {'%', 'f', 0} };
return reinterpret_cast<char const*>( "" );
}

auto main() -> int
{
printf( "'%s'\n", format_for( 5, 2, 1 ) );
}

它使 Visual C++ 2015 update 1 崩溃,ICE(内部编译器错误),并且 g++ 5.1.0 认为该函数只接受一个参数,大概是由于忽略了 匹配此模板参数失败后的 Args:

C:\my\forums\so\081> cl printf.cpp /Febprintf.cppprintf.cpp(14): fatal error C1001: An internal error has occurred in the compiler.(compiler file 'f:\dd\vctools\compiler\cxxfe\sl\p1\cxx\dymto.c', line 6771) To work around this problem, try simplifying or changing the program near the locations listed above.Please choose the Technical Support command on the Visual C++ Help menu, or open the Technical Support help file for more informationC:\my\forums\so\081> g++ printf.cppprintf.cpp: In function 'int main()':printf.cpp:14:43: error: no matching function for call to 'format_for(int, int, int)'     printf( "'%s'\n", format_for( 5, 2, 1 ) );                                           ^printf.cpp:4:6: note: candidate: template<class ... Args> const char* format_for(Args ..., int) auto format_for( Args... args, int last_arg )      ^printf.cpp:4:6: note:   template argument deduction/substitution failed:printf.cpp:14:43: note:   candidate expects 1 argument, 3 provided     printf( "'%s'\n", format_for( 5, 2, 1 ) );                                           ^C:\my\forums\so\081> _

所以,

  • 为什么上面的不能用g++编译?

  • 如何表达意图(希望从代码中明显看出)?

  • 更一般地说,匹配调用可变参数模板函数声明的规则是什么?

最佳答案

Args 出现在非推导上下文中。在这种情况下,这使得它被推断为 the the empty pack .

如果你想提取最后一个参数但保持通常的推导规则,你可以编写一个简单的帮助程序:

template <typename U>
constexpr U&& last(U&& u) {return std::forward<U>(u);}
template <typename U, typename... T>
constexpr decltype(auto) last(U&&, T&&... t) {return last(std::forward<T>(t)...);}

Demo .

More generally, what are the rules for matching a call to declaration of variadic template function?

这些非常冗长,但是尾随的函数参数包通常会被推导为空参数包或导致推导失败。


在您的特定情况下,尝试 index_sequences:

template <class... Args, std::size_t... indices>
auto format_for( std::index_sequence<indices...>, Args... args )
{
auto tup = std::forward_as_tuple(std::forward<Args>(args)...);
using Specifier = char const [3];
static Specifier const s[] = { {'%', (char)('f'+(0*std::get<indices>(tup))), ' '}..., {'%', 'f', 0} };
int last_arg = std::get<sizeof...(Args)-1>(tup);
return s;
}

template <class... Args>
auto format_for( Args&&... args ) {
return format_for(std::make_index_sequence<sizeof...(Args)-1>{},
std::forward<Args>(args)...);
}

...并希望编译器优化良好 - Demo 2 .或者走厚脸皮的路:

template <class... Args, std::size_t... indices>
auto format_for( std::index_sequence<indices...>, Args... args )
{
using Specifier = char const [3];
static Specifier const s[] = {
{'%', (char)(indices == sizeof...(Args)-1?
'f' : 'f'+(0*args)), ' '}... };
int last_arg = last(args...); // As before
return s;
}

template <class... Args>
auto format_for( Args&&... args ) {
return format_for(std::make_index_sequence<sizeof...(Args)>{},
std::forward<Args>(args)...);
}

Demo 3 .

关于c++ - 什么模式匹配(如果有)适用于 C++ 可变参数模板函数调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35068935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com