在 C++ 的 C++20 标准之前,当我们想要定义一个使用模板类的一些私有成员的类外运算符时,我们会使用类似于以下的构造:
template <typename T>
class Foo;
template <typename T>
constexpr bool operator==(T lhs, const Foo<T>& rhs);
template <typename T>
class Foo {
public:
constexpr Foo(T k) : mK(k) {}
constexpr friend bool operator==<T>(T lhs, const Foo& rhs);
private:
T mK;
};
template <typename T>
constexpr bool operator==(T lhs, const Foo<T>& rhs) {
return lhs == rhs.mK;
}
int main() {
return 1 == Foo<int>(1) ? 0 : 1;
}
Run Code Online (Sandbox Code Playgroud)
但是,从 C++20 开始,我们可以省略类外声明,因此也可以省略前向声明,因此我们可以只使用:
template <typename T>
class …Run Code Online (Sandbox Code Playgroud) 我抓我的头std::optional,而根据该文档,不应该有一个constexpr赋值运算符.
但是,当我在gcc-8.1中尝试这个片段时,它编译并正常工作:
constexpr std::optional<int> foo() {
std::optional<int> bar = 3;
bar = 1337;
return bar;
}
constexpr auto z = foo();
Run Code Online (Sandbox Code Playgroud)
有什么我想念的吗?
我正在开发一个与位于 VPS 上的数据库通信的应用程序。我需要在我的数据库中存储使用 AES-256 加密的信息。
如果我是对的,当我加密时,会生成一个 IV 参数,并且每个加密都不同。但是当我解密时,我没有这个参数,因为我只有密钥和数据库中的加密文本。
我能做些什么来解决这个问题?
在我的系统都ptrdiff_t和size_t是64位.
我想澄清两件事:
我相信没有阵列可以像size_t地址空间限制一样大.这是真的?
如果是,那么,是否有保证ptrdiff_t能够保存最大尺寸数组中任何指针的减法结果?
我想使用GLSL在我的32位位图纹理上实现透明度.我的片段着色器看起来像这样:
#version 120
uniform sampler2D myTexture;
varying vec2 TexCoord;
void main(void)
{
if(texture2D(myTexture, TexCoord).a != 1.0f)
{
discard;
}
gl_FragColor = texture2D(myTexture, TexCoord);
}
Run Code Online (Sandbox Code Playgroud)
但这使得透明只有alpha等于0的像素,我想保持纹理的淡入淡出.例如,在此图片中,第一个图像是纹理,第二个是纹理蒙版,第三个是期望的结果:

纹理和纹理掩码位于一个32位位图中.
有谁知道,如何使用GLSL实现这一目标?
最近我在不同的计算机上使用 GLSL 着色器版本时遇到了一些问题。我知道每个 GPU 对着色器都有不同的支持,但我不知道如何制作一个适用于所有 GPU 的着色器。如果我在我的 PC (GPU - AMD HD7770) 上编写一些着色器,我什至不必指定版本,但在一些较旧的 PC 或带有 nVidia GPU 的 PS 上,它对版本更严格,所以我必须指定GPU 支持的版本。
现在真正的问题来了。如果我在我的 PC 上指定例如版本 330,它可以正常工作,但在其他应该支持版本 330 的 PC 上它似乎不起作用。所以我必须重写它并使其工作。如果我切换回具有较新 GPU 的 PC,它也不起作用。
有谁知道,我必须如何编写着色器才能在所有 GPU 上运行?
我想用一种特定颜色裁剪图像,就像透明蒙版一样,并且我想创建包围图像的最小盒子。
一张图片可以更好地解释我的意思:
RGB(255,0,255) 是透明蒙版,绿色框代表所需的边界框。
我已经弄清楚了边界框的顶线:
int nAlloc = (128 * 128) * 4;
unsigned char* buf = new unsigned char[nAlloc];
GetBitmapBits(hBMP, nAlloc, buf);
for(int i = 0; i < nAlloc; i += 4)
{
if(buf[i] == 255 && buf[i + 1] == 0 && buf[i + 2] == 255)
{
continue;
}
// If I hit a first non-transparent pixel,
// I can then calculate the row where is that pixel located.
else if(set_pixel == false)
{
set_pixel = true; …Run Code Online (Sandbox Code Playgroud) 我想在C++中用VBO创建网格类.该课程如下:
mesh::mesh(std::vector<Vector3d>* Vertices, std::vector<unsigned int>* Indices, std::vector<Vector2d>* TextureCoords)
{
if(Vertices) this->vertices = *Vertices;
if(Indices) this->indices = *Indices;
if(TextureCoords) this->textureCoords = *TextureCoords;
chatbox.AddMessageToQueue("vertices.size() : %d", vertices.size());
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vector3d) + textureCoords.size()*sizeof(Vector2d), 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size()*sizeof(Vector3d), vertices.data());
glBufferSubData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vector3d), textureCoords.size()*sizeof(Vector2d), textureCoords.data());
glGenBuffers(1, &IND);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IND);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
}
mesh::~mesh()
{
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &IND);
}
void mesh::draw()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexPointer(3, GL_FLOAT, 0, (void*)0);
glTexCoordPointer(2, GL_FLOAT, 0, (void*)(sizeof(float)*3*6));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IND);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, …Run Code Online (Sandbox Code Playgroud)