为什么我的 constexpr 对象在我的函数中不是 constexpr?
Why is my constexpr object not constexpr inside my function?
我写了一个可以在编译时初始化和复制的类,并且在编译时使用函数之外的对象也可以工作。现在我想将我的constexpr
对象传递给函数以进行一些计算,但编译器产生错误,指出我的对象不是常量表达式。
如何编写函数才能使用constexpr
对象?
使用GCC 9.2、C++17(CLion 选项以使用 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...} {};
/* 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> >]’:
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
变量一起使用。
请注意,C++20 添加了类类型的非类型模板参数,这些参数可能会使引用以及链接的要求变得不必要(通过使用auto
而不是auto&
(,但这不适用于 C++20 草稿当前状态下的类,这要求该类型的所有非静态数据成员都是公共的。此要求最近发生了更改,因此当前编译器版本在使用-std=c++2a
时可能会实现以前草稿版本的不太严格的要求,其中您的类将是类类型的有效非类型模板参数。
不能在需要常量表达式的上下文中使用函数参数。此上下文出现在另一个常量表达式计算中这一事实并不重要。constexpr
函数必须始终是有效的运行时函数。
但是,在所示代码的特定情况下,您可以改为indices_amount
类的static
成员并通过类型访问该成员,以便函数参数的状态永远不会在常量表达式上下文中使用:
static constexpr std::size_t indices_amount = std::tuple_size_v<Args...>;
//...
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;
}
另请注意,您的tensorBase
将参数包作为第二个模板参数似乎没有意义。它只填充了一个参数,它也不能与任何其他数字一起使用。例如,仅当包只有一个元素时,std::tuple_size<Args...>
才有效。我建议您删除该类中与Args
相关的所有...
。
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的for循环不能正确获取argv
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 为什么我的 constexpr 对象在我的函数中不是 constexpr?
- constexpr 函数在编译时获取值,即使我的变量不是 constexpr
- 我可以制作一个std::set的constexpr对象吗
- 为什么 constexpr 假设我的方法就是 const
- 如果我的班级是字面的班级,那么将我的类的对象声明为constexpr是多余的
- 为什么我的编译器显示有关我的 constexpr 函数的此错误?
- 如何使我的C 编译器发出constexpr值(例如警告?)
- 当我击中不良的constexpr时,请停止Visual Studio 17编译
- 为什么我的 constexpr 函数不能返回 lambda?