我正在玩弄concepts,我试图定义一个concept可以接受任何值non-type parameter的函数,然后函数会使用关键字后跟the 来isUnsignedInt检查参数是否为an 。unsigned intrequiredconcept
问题是,我可以传递一个负整数,并且没有错误消息,类型不是unsigned int。
我对概念的理解错了吗?
目前,我正在使用gcc 9.2 compiler和我的CMake contains add_compile_options(-fconcepts),它启用了concepts。
码:
template<auto a>
concept special = requires(){
{a} -> unsigned int;
};
template<auto a> requires special<a>
constexpr auto isUnsignedInt(){
return a;
}
int main(){
std::cout << isUnsignedInt<-2>() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
-2
Run Code Online (Sandbox Code Playgroud) 是否可以创建一个输入,作为函数的参数重复 N 次?
一个例子:
#include <range/v3/view/indices.hpp>
#include <range/v3/view/cartesian_product.hpp>
template<std::size_t length, std::size_t N>
constexpr auto tensor_cartesian_product(){
const auto cart_input1 = ranges::view::indices(length); //create input
return ranges::view::cartesian_product(cart_input1, cart_input1, ... /* N times cart_input1 */);
}
Run Code Online (Sandbox Code Playgroud) 我想使用一个类中的模板参数,以实例化具有相同模板参数的另一个对象。我试图将参数保存在结构体中,但无法将类型用于新对象。
template<typename D>struct Typename {
using myType = D;
};
template<typename T>
class example{
public:
Typename<T> someType;
};
int main(){
example<double> e1;//ok
example<e1.someType.myType> e2; //error: cannot refer to member 'myType'
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
error: invalid use of ‘using myType = double’
example<e1.someType.myType> e2;
^~~~~~
error: template argument 1 is invalid
example<e1.someType.myType> e2;
^
error: conflicting declaration ‘example<double> e2’
example<typename decltype(e1.someType)::myType> e2; //error: cannot refer to member 'myType'
^~
Run Code Online (Sandbox Code Playgroud) 我编写了一个可以在编译时初始化和复制的类,并且在我的函数之外使用该对象也可以在编译时工作。现在我想将我的constexpr对象传递给一个函数来做一些计算,但是编译器产生一个错误,我的对象不是一个常量表达式。
我必须如何编写函数才能使用我的constexpr对象?
Using GCC 9.2, C++17, (CLion option to use C++20已激活)
这是我的课程,我的功能和主要内容。注意:并不是所有的定义都给出了,因为它会为一篇文章写很多代码。
template<std::size_t t1_skipPos, std::size_t t2_skipPos, typename T1, typename T2>
constexpr auto contraction(T1 tensor1, T2 tensor2){
/*ERROR: tensor1 is not a constant expression*/
auto sris_tensor1 = save_recreated_index_sequence<0, tensor1.indices_amount-2,
t1_skipPos, tensor1.indices_amount, DIM3>(tensor1.calculate_indices());
return sris_tensor1;
}
template<typename T, typename ... Args>
class tensorBase{
private:
std::array<T, positive_natural_compiletime_pow<DIM3, std::tuple_size<Args...>::value>()> data;
public:
//std::vector<T> data = {};
std::tuple<Args...> myTypeTup;
std::size_t indices_amount = std::tuple_size<Args...>::value;
template<typename ... Element>
constexpr tensorBase(Element&&... input) : data{input...} {}; …Run Code Online (Sandbox Code Playgroud) 我的函数采用两个数组,它们存储具有原始数据类型的数据。
我想为我的result数组选择更大的数据类型,以防止在将值插入数组时丢失数据。
例如:第一个数组存储整数和第二个双精度。该result阵列应该有一倍。
template<std::size_t length, typename A1, typename A2>
constexpr auto function(A1 array1, A2 array2){
//e.g array1 ints, array2 doubles
//do calculations and insert values into result array
std::array< ???, length> result{};
return array3;
}
Run Code Online (Sandbox Code Playgroud)