小编Chi*_*iel的帖子

使用auto关键字创建的类型在表达式模板中分段错误

我正在使用计算内核的表达式模板构建代码.我的问题非常简短:为什么GNU G ++在包含+=以下示例的行中给出了段错误(4.9.1,使用-O3编译):

// Like this it crashes
auto expression = Ix_h( Ix(u) );
ut += expression;
Run Code Online (Sandbox Code Playgroud)

但是,当我输入等效代码时:

// But like this it does not
ut += Ix_h( Ix(u) );
Run Code Online (Sandbox Code Playgroud)

Clang和Intel都运行良好.

我在下面添加了完整的代码.抱歉长度,这是我可以创建的最短的例子:

struct Grid
{
  Grid(const int itot, const int gc) :
    itot(itot), gc(gc), istart(gc), iend(itot+gc), icells(itot+2*gc) {}

  const int itot;
  const int gc;
  const int istart;
  const int iend;
  const int icells;
};

template<int loc, class Inner>
struct Interp
{
  Interp(const Inner& inner) : inner_(inner) {}

  const …
Run Code Online (Sandbox Code Playgroud)

c++ segmentation-fault expression-templates auto c++11

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

如何避免受保护的班级成员?

我在许多讨论中都读到,有protected班级成员是坏人,我看到了原因.但是考虑到下面的例子,最优雅的方法是什么,const int age private并解决由此产生的问题?

#include <iostream>

class Animal
{
    public:
        Animal(const int age) : age(age) {}
        void print_age() const { std::cout << age << std::endl; }
    protected:
        const int age;
};

class Dog : public Animal
{
    public:
        Dog(const int age) : Animal(age) {}
        void bark() const
        {
            if (age >= 1)
                std::cout << "Woof!" << std::endl;
            else
                std::cout << "(...)" << std::endl;
        }
};

int main()
{
    Dog dog(1);
    dog.print_age();
    dog.bark();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ oop protected

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

在某些 C++ 编译器上使用限制限定符的编译器错误

我有一个函数,它的指针为doubleas 参数,被限定为restrict. 请注意,英特尔编译器使用restrict,但__restrict__在 GCC 的情况下,我们将限定符替换为。

void Stats::calc_flux_2nd(double* restrict data,
                          double* restrict datamean,
                          double* restrict w)
{
    // ...

    // Set a pointer to the field that contains w,
    // either interpolated or the original
    double* restrict calcw = w;

    // ...
Run Code Online (Sandbox Code Playgroud)

使用 GCC 或 Clang 编译此代码没有任何问题,但 IBM BlueGene 编译器给出以下错误:

(W) Incorrect assignment of a restrict qualified pointer.
Only outer-to-inner scope assignments between restrict pointers are 
allowed.  This may result in incorrect program …
Run Code Online (Sandbox Code Playgroud)

c++ restrict

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

将派生类型中的指针分配给 Fortran 中相同类型中的目标

我想在包含在同一派生类型中的派生类型中分配一个指针。下面的代码给了我下面的错误。这是怎么回事,我该如何解决这个问题?

   24 |         zoos(i)%tigers(1) => zoos(i)%animals(1, 1)
      |        1
Error: Expected bounds specification for 'zoos' at (1)
Run Code Online (Sandbox Code Playgroud)
module mo_zoo
    implicit none
    type zoo
        integer, dimension(:,:), pointer :: animals
        integer, dimension(:), pointer :: tigers
        integer, dimension(:), pointer :: ducks
    end type zoo
   
    save
        type(zoo), dimension(:), pointer :: zoos
end module mo_zoo

program test
    use mo_zoo
    implicit none
    integer :: n_zoos
    integer :: i

    n_zoos = 4
    allocate(zoos(n_zoos))

    do i = 1, n_zoos
        allocate(zoos(i)%animals(10, 2))
        zoos(i)%tigers(1) => zoos(i)%animals(1, 1)
        zoos(i)%ducks(1) => zoos(i)%animals(1, …
Run Code Online (Sandbox Code Playgroud)

fortran

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

如何设计一个灵活的C++程序,派生类在其中进行交互?

我希望这里的专家对以下问题有一个看法.我想设计一个程序,其中来自不同基类的派生类相互交互.为了明确我的观点,我已经构建了一个例子.

想象一下,我试图建立一个动物园.在这个动物园里,我们有动物,我们有水果,这是我的基础类.只有在运行时我们才知道我们拥有哪些动物,以及哪些动物.假设我有一只猴子和一根香蕉.

我怎么让这只猴子以优雅的方式吃香蕉,吃香蕉只是这种猴子非常独特的技能?请注意,我不想要一个通用的吃功能,因为在我的设计中我需要在衍生动物可以执行的动作中完全自由.

这是随附的例子:

#include <string>
#include <iostream>

class cfruit
{
};

class cbanana : public cfruit
{
};

class canimal
{
};

class cmonkey : public canimal
{
  public:
    int eatbanana(cbanana *) { std::cout << "BURP" << std::endl; }
};

int main()
{
  cfruit  *fruit;
  canimal *animal;

  // on runtime the animal and the fruit is decided, so we have to 
  // initialize it on the base pointer

  // assume we get a monkey and a banana
  fruit  = …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance class

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

使用C++类中的复杂函数初始化const成员

我有一个与3d网格一起使用的程序.这个网格有自己的类对象Grid,看起来像这样(简化版):

class Grid
{
  public:
    Grid() { readDataFromInputFile(); }
  private:
    void readDataFromInputFile() {...} // this function reads the values for i, j, k from disk
    int i; int j; int k;
};
Run Code Online (Sandbox Code Playgroud)

我现在想做的是能够将变量i,j和k设置为const int,以避免在其他函数中意外地搞乱它们.但是我不能在成员初始化列表中轻松设置它们,因为它们必须从文件中读取.我一直在浏览现有的讨论,但我找不到关于这个问题的准确讨论.

有没有一个解决方案能够将它们设置为const,并且仍然能够使用复杂的函数来检索数据?在我的实际应用程序中,当然还有更多要读取的变量,初始化后不允许更改.

c++ class initialization-list

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

模板化类中的模板化函数

我正在构建一个使用表达式模板的库,我在类中大量使用模板化函数.我的所有代码都在运行,最近我决定将主类设置为允许在不同类型的数据上使用它.但是,我再也无法专注于我的功能了.我该如何解决这个问题?我附上了一个显示问题的小测试程序.

我以前的Animal课程没有模板化,然后这段代码工作正常.

#include<iostream>
#include<vector>

// Example templated class with templated function
template<class T>
class Animals
{
  public:
    template<class X>
    void printFood(const X& x) const { std::cout << "We eat " << x << "!" << std::endl; }

  private:
    std::vector<T> animals;
};

// How do I specialize?
template<class T> template<>
void Animals<T>::printFood(const unsigned int& x) const { std::cout << "We don't want to eat " << x << "!" << std::endl; }

// Main loop;
int main()
{
  Animals<double> …
Run Code Online (Sandbox Code Playgroud)

c++ templates expression-templates

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

赋值中 C++14 中的返回类型推导

我想知道在 C++14 中是否可以以某种方式在赋值中进行返回类型推导。<int>return_five函数名之后输入 感觉多余。因此,换句话说,编译器可以使用赋值左侧的信息吗?

#include <iostream>
#include <string>

template<typename T>
auto return_five()
{
    return static_cast<T>(5);
}

int main()
{
    int five_int = return_five();         // THIS DOES NOT WORK
    // int five_int = return_five<int>(); // THIS WORKS

    std::cout << "Five_int = " << five_int << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ return-type c++14

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

将std :: vector &lt;bool&gt;传递给外部函数

我正在处理无法更改数据类型的库。我需要调用一个需要bool数组的函数。我正在使用的代码std::vector<bool>用于存储布尔值。我已经阅读了很多有关std::vector<bool>SO的问题以及与之相关的问题,但是我还没有找到针对我的问题的最优雅的解决方案。这是性能至关重要的代码。如何以最有效的方式处理此问题?我可以更改存储在中的类型std::vector,但不能逃避使用std::vector

对于我自己的问题,我必须调用Fortran函数,但是我在C ++中做了一个最小的例子来说明问题。

#include <cstdio>
#include <vector>

void print_c_bool(bool* bool_array, size_t n)
{
    for (int i=0; i<n; ++i)
        printf("%d, %d\n", i, bool_array[i]);
}

int main()
{
    // This works.
    bool bool_array[5] = {true, false, false, true, false};
    print_c_bool(bool_array, 5);

    // This is impossible, but how to solve it?
    std::vector<bool> bool_vector {true, false, false, true, false};
    // print_c_bool(bool_vector.data(), bool_vector.size());

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ boolean hpc stdvector

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

检查指定给std :: array编译时间的元素数

我有一个性能关键代码,可以查询N个索引。如何static_assert在不牺牲性能的情况下检查是否给出了准确的N个索引?

#include <array>

template<int N>
void test(const std::array<int, N>& indices)
{
    // static_assert:  has three elements.
    return;
}

int main()
{
    test<3>({1, 2, 3}); // OK
    test<3>({1, 2});    // Needs to crash, because 2 < 3

    test<2>({1, 2, 3}); // Crashes, because 3 > 2
    test<2>({1, 2});    // OK

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ static-assert stdarray

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

视图上的融合运算符比 Julia 中的嵌套循环慢得多

我正在尝试创建一个函数,通过使用view和融合运算符尽可能快地计算扩散内核。是否有可能像第一个函数一样快速地获得第二个函数?目前,diff需要 59.6 毫秒,而diff_view需要 384.3 毫秒。

using BenchmarkTools


function diff(
        at::Array{Float64, 3}, a::Array{Float64, 3},
        visc::Float64, dxidxi::Float64, dyidyi::Float64, dzidzi::Float64,
        itot::Int64, jtot::Int64, ktot::Int64)

    for k in 2:ktot-1
        for j in 2:jtot-1
            @simd for i in 2:itot-1
                @inbounds at[i, j, k] += visc * (
                    (a[i-1, j  , k  ] - 2. * a[i, j, k] + a[i+1, j  , k  ]) * dxidxi +
                    (a[i  , j-1, k  ] - 2. * a[i, j, k] + a[i …
Run Code Online (Sandbox Code Playgroud)

performance julia

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

Python Matplotlib 散点图以不一致的数字绘制轴

我正在绘制月份与温度(y 轴)的数据集,我使用:

plt.scatter(time, temps)
plt.show()
Run Code Online (Sandbox Code Playgroud)

但在图中,y 轴不一致(点 8 的值为 19.5749,但显示的值高于前一点的 22.482) matplotlib 图

python matplotlib scatter-plot

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

此代码块是否会导致内存泄漏?

此代码块是否会导致内存泄漏?

char * foo = new char [20];
read(STDIN_FILENO, foo, 20);
string bar;
bar.reserve(20);
bar = foo;
delete[] foo;
Run Code Online (Sandbox Code Playgroud)

我认为它不能,因为我们delete[]用来释放大块的内存.但是,对象bar可能会有所不同.请分享你的观点.

c++ memory

0
推荐指数
1
解决办法
161
查看次数