使用 C++20,可以为别名模板生成推导准则(请参阅https://en.cppreference.com/w/cpp/language/class_template_argument_deduction 上的“别名模板的推导”部分)。然而,我无法让它们使用聚合初始化语法。在这种情况下,似乎没有生成别名的扣除指南。
看这个例子:
#include <array>
template <size_t N>
using mytype = std::array<int, N>;
// Deduction guideline ???
int main() {
// mytype error_object = {1, 4, 7}; // ERROR
mytype<3> object = {1, 4, 7}; // OK, but I have to manually specify the size.
return object[0];
}
Run Code Online (Sandbox Code Playgroud)
我曾尝试编写演绎指南,但每次都会出现编译器错误。
template <typename T, typename ... U>
mytype(T, U...) -> mytype<1+sizeof...(U)>; // Compiler error
Run Code Online (Sandbox Code Playgroud)
以及我能想到的任何其他准则。
甚至可以自动推导出数组别名的大小吗?
我正在使用 GCC 10.2
c++ aggregate-initialization template-aliases c++20 deduction-guide
在以下 C++ 代码中, function 的参数fun1和 function 的返回类型中的模板占位符ret1无法编译:
template <typename T = int>
class type {
T data;
};
void fun1(type arg); // Error: template placeholder not permitted in this context
void fun2(type<> arg); // Ok
void fun3(type<int> arg); // Ok
type ret1(); // Error: Deduced class type 'type' in function return type
type<> ret2(); // Ok
type<int> ret3(); // Ok
int main() {
type var1; // Ok!!!!!!
type<> var2; // Ok
type<int> var3; // Ok
} …Run Code Online (Sandbox Code Playgroud) 以下 C++17 代码片段在 GCC 和 CLang 中进行编译,但在 Visual C++ 中会出现以下错误:
<source>(14): error C2672: 'f': no matching overloaded function found
<source>(14): error C2784: 'std::ostream &f(std::ostream &,const container<int> &)': could not deduce template argument for 'const container<int> &' from 'const std::vector<int,std::allocator<int>>'
<source>(5): note: see declaration of 'f'
Run Code Online (Sandbox Code Playgroud)
https://godbolt.org/z/aY769qsfK
#include <vector>
template< template <typename...> typename container >
void f (const container< int > &)
{ }
int main()
{
std::vector<int> seq = {1, 2, 3};
f<std::vector>(seq); // OK
f(seq); // ERROR
}
Run Code Online (Sandbox Code Playgroud)
请注意,此代码类似于 …
c++ templates template-templates template-argument-deduction c++17
当我在 Linux 内核 v5.14 上连接游戏手柄时,会显示两个新设备:
/dev/input/event23
/dev/input/js1
Run Code Online (Sandbox Code Playgroud)
如果我cat <file> | xxd两个设备文件都提供游戏手柄事件信息。但event23比 更加冗长js1。
另外,在 上evtest给出错误,但在 上工作正常。当我使用 libevdev 两个设备文件时,也会发生同样的情况。Invalid Argumentjs1event23
看起来每个事件都会js1转储结构的内容input_event(在 中定义linux/input.h)
设备文件之间有什么区别?为什么他们有不同的信息以及event23提供了哪些更多信息js1?