小编M.M*_*Mac的帖子

C ++,概念不能将无符号整数用作结果类型?

我正在玩弄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)

c++ c++-concepts

5
推荐指数
1
解决办法
72
查看次数

C++,多次重复一个函数的输入

是否可以创建一个输入,作为函数的参数重复 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)

c++ templates

5
推荐指数
1
解决办法
242
查看次数

使用返回的模板参数创建对象失败,为什么?

我想使用一个类中的模板参数,以实例化具有相同模板参数的另一个对象。我试图将参数保存在结构体中,但无法将类型用于新对象。

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)

c++ templates

3
推荐指数
1
解决办法
60
查看次数

如何从 github 安装 C++20 range 库

我想在我的项目中使用range-v3库,但我不明白如何使用。安装说明如下:

该库仅包含标头。您可以从 github 上的 range-v3 存储库获取源代码。要使用 Range-v3 进行编译,只需#include 您想要的各个标头。

这是否意味着我可以复制并粘贴所需的头文件并将文件路径添加到我的 CMake 文件中?我有点困惑,因为我从未包含第三方库。

c++ cmake range-v3

2
推荐指数
1
解决办法
2285
查看次数

为什么我的 constexpr 对象在我的函数中不是 constexpr?

我编写了一个可以在编译时初始化和复制的类,并且在我的函数之外使用该对象也可以在编译时工作。现在我想将我的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)

c++ templates constexpr

2
推荐指数
1
解决办法
103
查看次数

如何为我的数组选择更大的数据类型?

我的函数采用两个数组,它们存储具有原始数据类型的数据。

我想为我的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)

c++ arrays types

2
推荐指数
1
解决办法
62
查看次数

标签 统计

c++ ×6

templates ×3

arrays ×1

c++-concepts ×1

cmake ×1

constexpr ×1

range-v3 ×1

types ×1