我写了一个可以在编译时初始化和复制的类,并且使用我函数外部的对象在编译时也可以工作。现在,我想将我的constexpr
对象传递给函数进行一些计算,但是编译器会产生一个错误,表明我的对象不是常量表达式。
如何编写函数才能使用我的constexpr
对象?
使用GCC 9.2,C ++ 17,(激活了使用C ++ 20的CLion选项)
这里是我的班级,我的职能和主要人员。注意:并不是所有的定义都给出了,因为一个职位的代码太多。
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...} {};
/* copy constructor */
template<typename Tensor>
constexpr tensorBase(const Tensor &oldObj){
data = oldObj.get_data();
myTypeTup = oldObj.myTypeTup;
indices_amount = oldObj.indices_amount;
}
/*
template<typename Arr>
constexpr tensorBase(Arr&& array) {
data = array;
};
*/
constexpr auto calculate_indices() const{
static_assert((std::tuple_size<Args...>::value <= 5), "tensor has to many indices");
//auto l = std::tuple_size<Args...>::value;
return cartesian_product<DIM3, std::tuple_size<Args...>::value>();
//return cartesian_product_to_vec<DIM3, sizeof...(Args)+1>();
};
template<typename D>
constexpr auto get_element(D&& val) const{
return data[val];
};
constexpr auto get_data() const{
return data;
};
};
template<typename T, typename ... Args>
using tensor = tensorBase<T, std::tuple<Args...>>;
int main(){
constexpr tensor<double, up_t, low_t, low_t> tensor1(11.0, 22.0, 33.0, 44.0, 55.0, 66.0, 77.0, 88.0, 99.0); /*OK*/
constexpr tensor<double, up_t, low_t> tensor2(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9);/*OK*/
constexpr auto copyObj1 = tensor1; /*OK*/
constexpr auto indices = tensor1.indices_amount; /*OK*/
constexpr auto indices1 = tensor1.calculate_indices(); /*OK*/
constexpr auto sris_outside = save_recreated_index_sequence
<0, tensor1.indices_amount-1,0,tensor1.indices_amount,2>(tensor1.calculate_indices()); /*OK*/
constexpr auto contract = contraction<0,0>(tensor1, tensor2); /*Error*/
return 0;
}
错误消息:
In instantiation of ‘constexpr auto contraction(T1, T2) [with long unsigned int t1_skipPos = 0; long unsigned int t2_skipPos = 0; T1 = tensorBase<double, std::tuple<up_t, low_t, low_t> >; T2 = tensorBase<double, std::tuple<up_t, low_t> >]’:
/home/martin/CLionProjects/tensor-library-v1/main.cpp:162:64: required from here
error: no matching function for call to ‘save_recreated_index_sequence<0, (tensor1.tensorBase<double, std::tuple<up_t, low_t, low_t> >::indices_amount - 2), 0, tensor1.tensorBase<double, std::tuple<up_t, low_t, low_t> >::indices_amount, DIM3>(std::array<std::tuple<long unsigned int, long unsigned int, long unsigned int>, 27>)’
61 | auto sris_tensor1 = save_recreated_index_sequence<0, tensor1.indices_amount-2, t1_skipPos, tensor1.indices_amount, DIM3>(tensor1.calculate_indices());
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: candidate: ‘template<long unsigned int offset, long unsigned int N, long unsigned int skipPos, long unsigned int length, long unsigned int times, class Arr> constexpr auto save_recreated_index_sequence(Arr&&)’
46 | constexpr auto save_recreated_index_sequence(Arr&& arr){
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: template argument deduction/substitution failed:
In file included from /home/martin/CLionProjects/tensor-library-v1/main.cpp:43:
error: ‘tensor1’ is not a constant expression
61 | auto sris_tensor1 = save_recreated_index_sequence<0, tensor1.indices_amount-2, t1_skipPos, tensor1.indices_amount, DIM3>(tensor1.calculate_indices());
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in template argument for type ‘long unsigned int’
61 | auto sris_tensor1 = save_recreated_index_sequence<0, tensor1.indices_amount-2, t1_skipPos, tensor1.indices_amount, DIM3>(tensor1.calculate_indices());
| ~~~~~~~~~~~~~~~~~~~~~~^~
error: ‘tensor1’ is not a constant expression
61 | auto sris_tensor1 = save_recreated_index_sequence<0, tensor1.indices_amount-2, t1_skipPos, tensor1.indices_amount, DIM3>(tensor1.calculate_indices());
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in template argument for type ‘long unsigned int’
61 | auto sris_tensor1 = save_recreated_index_sequence<0, tensor1.indices_amount-2, t1_skipPos, tensor1.indices_amount, DIM3>(tensor1.calculate_indices());
| ~~~~~~~~^~~~~~~~~~~~~~
error: unable to deduce ‘auto’ from ‘sris_tensor1’
63 | return sris_tensor1;
| ^~~~~~~~~~~~
In function ‘int main()’:
error: ‘constexpr const void contract’ has incomplete type
162 | constexpr auto contract = contraction<0,0>(tensor1, tensor2);
| ^~~~~~~~
如果要在需要常量表达式的上下文中使用它,则需要将tensor1
作为模板参数传递。当前(C ++ 17)仅可作为参考:
template<std::size_t t1_skipPos, std::size_t t2_skipPos, auto& tensor1, auto& tensor2>
constexpr auto contraction(){
auto sris_tensor1 = save_recreated_index_sequence<0, tensor1.indices_amount-2,
t1_skipPos, tensor1.indices_amount, DIM3>(tensor1.calculate_indices());
return sris_tensor1;
}
//...
constexpr auto contract = contraction<0, 0, tensor1, tensor2>();
但是,这还要求您将tensor1
和tensor2
声明为static
,以便它们具有链接。否则,它们不能用作模板参数。
您不能在需要常量表达式的上下文中使用函数参数。该上下文出现在另一个常量表达式评估中的事实并不重要。 constexpr
函数还必须始终是有效的运行时函数。
但是在所示代码的特定情况下,您可以将indices_amount
设置为类的static
成员,并通过以下类型进行访问:
static constexpr std::size_t indices_amount = std::tuple_size<Args...>::value;
//...
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, T1::indices_amount-2,
t1_skipPos, T1::indices_amount, DIM3>(tensor1.calculate_indices());
return sris_tensor1;
}