小编tow*_*120的帖子

OpenGL多线程/共享上下文和glGenBuffers

如果我计划在OpenGL中使用多线程,我应该为每个上下文分别使用缓冲区(来自glGenBuffers)吗?

我对OpenGL多线程还不太了解(现在我在"单一"线程中工作).我需要知道我是否可以共享已经推送到Video Memory的缓冲区(使用glBufferData/glBufferSubData),或者我必须保留另一个线程的缓冲区副本.

c++ opengl multithreading

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

C++17 中指向 constexpr 静态成员的 constexpr 指针

在下面的代码中,我将地址分配给静态 constexpr 成员:

struct component_type_data{};

template<class Derived>
class component{
    private:
    const constexpr static component_type_data type_data{};
    public:
    static constexpr const component_type_data* component_type = &type_data;
};
Run Code Online (Sandbox Code Playgroud)

我的动机是让编译时类型有唯一的 id。

这是有效的吗?代码仅从 C++17 开始编译。我可以使用该指针作为模板参数。

如果这有效,编译器如何预先知道地址?

更新 :

另外,跨 dll 边界会发生什么?每个 dll 对于相同的静态成员都有自己唯一的地址,或者它们将是相同的?

c++ c++17

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

C++ 可变说明符

什么时候可以,什么时候不能调用可变变量?

int/float/bool 值非常清楚。但是,比方说,数组呢?mutable如果我要向它添加元素,我可以调用 native array吗?与std::vector.

再举一个例子。我有对象 A,它保持对另一个对象 B 的引用 (B & b)。对象 B 有我将重新分配的本机数组/ std::vector (我认为在这种特殊情况下是类似的)。伪代码:

struct B{
   std::vector<int> arr;
   // int *arr;              //Or this
   void changeArr(){
     arr.push_back(90);
   }
}

struct A{
   A(B &b) : b(b){};
   mutable B &b;     // is it ok to have it "mutable"?
   //mutable B b;    // or even this?
   void fire() const{
      b.arr.push_back(125);
      // Or 
      b.changeArr();
   }
}
Run Code Online (Sandbox Code Playgroud)

我可以调用B &b可变的吗?


更新

根据http://en.cppreference.com/w/cpp/language/cv

可变 - 定义类的成员不影响类的外部可见状态。

这是什么externally …

c++ mutable

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

C++缓存向量空()

缓存vector.empty()价值是否有意义?

Vector<X> list;
...
if(!list.empty()){          // cmp + jnz
   ...
   // some heavy calculations
   ...
   list.pop_back();
}
Run Code Online (Sandbox Code Playgroud)

VS

Vector<X> list;
bool list_empty;
...
if(!list_empty){            // jnz
   ...
   // some heavy calculations
   ...
   list.pop_back();
   list_empty = list.empty();   // cmp
}
Run Code Online (Sandbox Code Playgroud)

哪里

bool List::empty(){
  return last_ptr == first_ptr;         // implementation from gcc's std
}
Run Code Online (Sandbox Code Playgroud)

我认为缓存更好,因为在list不为空的情况下,只需要1 op进行比较.另外cmp,它应该从内存中获取两个值,而for jnz,只有一个.

PS Vector就是一个很好的例子.这种逻辑可以出现在另一种情况中.

c++

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

C++ static_cast向下转换有效性

这种static_cast贬低有效吗?

// non-virtual, may be non-trivially copyable
struct Base{
    int m_object;
};

// Derived class have only non-virtual functions
struct A : Base{
    void arggh(){
        std::cout << "Arrghh " << m_object;
    }
};

int main() {
    Base base{190};
    A& a = static_cast<A&>(base);
    a.arggh();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的意思是,CREATE基类,然后转换为派生.

生活

c++

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

C++多接口继承和static_cast

在以下代码中:

生活

#include <iostream>
#include <thread>
#include <mutex>
#include <functional>

struct IView {
    virtual void setOnClick() = 0;
};
struct ITextView : IView {
    virtual void setText() = 0;
};
struct IButton : ITextView {
    virtual void setRadius() = 0;
};

struct View : IView {
    int i = 1;
    virtual void setOnClick() override {
        std::cout << "setting OnClick! i: " << i << std::endl;
    };
};

/// Works as is
/// But if make "TextView : View, ITextView" - …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance casting multiple-inheritance

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

带有rvalue param调用的C++ 11模板化函数

在某些O类中,我有模板化的函数test2:

struct A{int value;};

struct O{
    A value;

    template<typename Args>
    static void test2(Args &&args){
        std::cout << std::endl << "!!!" << std::is_rvalue_reference<decltype(args)>::value << std::endl;
    }
};
Run Code Online (Sandbox Code Playgroud)

比,我想从另一个调用此函数:

template<typename Args>
void test(Args &&args){
    using t = decltype(std::forward<Args>(args).value);
    std::cout << std::is_rvalue_reference<decltype(args)>::value;
    std::cout << std::is_rvalue_reference<decltype(std::forward<Args>(args).value)>::value;
    std::cout << std::is_rvalue_reference<t>::value;

    // All ok
    O.test2(std::forward<Args>(args).value);            

    // Alvays rvalue, even if agrs is lvalue
    O::template test2<t>(
        std::forward<t>(
            std::forward<Args>(args).value
        )
    );

   // Nor work at all, cant cast A to A&&
   O::template test2<t>(
        std::forward<Args>(args).value
    );
);

} …
Run Code Online (Sandbox Code Playgroud)

c++ templates rvalue-reference c++11

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

使用framebuffer对象作为着色器中的纹理

我可以使用FBO blitting https://www.opengl.org/registry/specs/EXT/framebuffer_blit.txt,将渲染的FBO作为纹理传递给我的解析着色器输入吗?

我可以使用FBO作为着色器"sampler2D"的输入吗?

我的意思是,我为fbo渲染了一些东西.现在我想要后处理图像,我可以将FBO作为纹理传递到着色器吗?

opengl fbo

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

在variadic pack之前的C++ 11默认模板参数

为什么模板类参数在参数之后具有默认值,但在variadic之前也必须具有默认值? 实例

template<class A = int, class B, class ...Args>     // B must have default argument
struct Dat{};
Run Code Online (Sandbox Code Playgroud)

另一方面,如果A没有默认参数,那么ok:

template<class A, class B, class ...Args>         // B must have default argument
struct Dat{};
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++11

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

可变模板循环,非递归

我有一些功能,可以执行复杂的绘图。[伪代码]

template<typename fields...>       // field names of Brush class
void someFunction(){    
  for(very large loop){

     Brush brush = getBrush();
     int x;

     foreach(field : fields){     // <--- this somehow should be replaced
        x = brush.*field;
        brush.update(x);
     }

  }    
}
Run Code Online (Sandbox Code Playgroud)

[清单 1]

我称之为:

someFunction<&Brush::xPos1, &Brush::xPos2, &Brush::xPos3, &Brush::xPos4>()
Run Code Online (Sandbox Code Playgroud)

我希望编译器生成这样的东西:

void someFunction(){    
  for(very large loop){

     Brush brush = getBrush();
     int x;

        x = brush.xPos1;
        brush.update(x);

        x = brush.xPos2;
        brush.update(x);

        x = brush.xPos3;
        brush.update(x);

        x = brush.xPos4;
        brush.update(x);

  }    
}
Run Code Online (Sandbox Code Playgroud)

[清单 2]

我的意思是,我想摆脱那个 foreach(field : fields)。 …

c++ templates variadic-templates

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