Qt C++ 在函数模板中使用约束
Qt C++ using constraints in function templates
我尝试使用 Qt 声明函数模板以仅计算 quint8
、quint16
和 quint32
的某些值。
我写道:
template<typename T>
concept CrcCompatible = typeid(T) == typeid(quint8) || typeid(T) == typeid(quint16) || typeid(T) == typeid(quint32);
template<typename T> requires CrcCompatible<T>
T crc(T initial, T polynomial, T *pcBlock, T len, bool isFinalXor);
但是 concept
和 requires
关键字不会作为语法的一部分突出显示。当我尝试编译此代码时出现以下错误:
concept does not name a type
...
requires does not name a type
我第一次尝试在 Qt 中使用模板。我不明白如何修复此错误,以及为什么编译器无法理解关键字。
在项目文件中我添加了 QMAKE_CXXFLAGS += -std=c++11
和 CONFIG += c++11
但什么都没有改变。
我不明白我应该在google中问什么,所以我找不到答案...
我想问题是 C++11 不支持概念。据我在 documentation, the concepts will only be available in C++20 中所见。无论如何,如果现在某些编译器支持它们,您应该通过 --std=c++11
.
以外的其他标志启用此支持
我尝试使用 Qt 声明函数模板以仅计算 quint8
、quint16
和 quint32
的某些值。
我写道:
template<typename T>
concept CrcCompatible = typeid(T) == typeid(quint8) || typeid(T) == typeid(quint16) || typeid(T) == typeid(quint32);
template<typename T> requires CrcCompatible<T>
T crc(T initial, T polynomial, T *pcBlock, T len, bool isFinalXor);
但是 concept
和 requires
关键字不会作为语法的一部分突出显示。当我尝试编译此代码时出现以下错误:
concept does not name a type
...
requires does not name a type
我第一次尝试在 Qt 中使用模板。我不明白如何修复此错误,以及为什么编译器无法理解关键字。
在项目文件中我添加了 QMAKE_CXXFLAGS += -std=c++11
和 CONFIG += c++11
但什么都没有改变。
我不明白我应该在google中问什么,所以我找不到答案...
我想问题是 C++11 不支持概念。据我在 documentation, the concepts will only be available in C++20 中所见。无论如何,如果现在某些编译器支持它们,您应该通过 --std=c++11
.