小编Rai*_*dex的帖子

我可以禁止临时对象作为参数吗?

假设我有这个功能:

void foo(Object& o) {
    /* only query o, dont alter it*/
}
Run Code Online (Sandbox Code Playgroud)

是否可以仅使用已构造的对象调用此函数,并且如果我使用临时对象调用该函数,Visual Studio 会抛出编译错误?

struct Object {
    /*Members*/
}

void foo(Object& o) {
    /* only query o, dont alter it*/
}

int main() {
    Object o = Object();
    foo(o); // allow this
    foo(Object()) // but disallow this
}
Run Code Online (Sandbox Code Playgroud)

c++ function visual-studio move-semantics pass-by-rvalue-reference

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

CAMetalLayer 仅适用于 iOS 13.0

我正在使用CAMetalLayer绘制到 UIView 上。编译器会抛出一个CAMetalLayer仅在 iOS 13.0 上可用的警告。然而
文档iOS 8.0+
我可以忽略这个警告吗?我这里只有 iOS 13.0 设备,我们也想支持 12 和 11。

在此处输入图片说明

rendering ios metal

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

RealityKit – 材质的 Alpha 透明度

纹理是否可以具有 alpha 透明度?

我有包含 8 位 RGBA 的 png 文件,但由于某种原因,应该透明的部分只是黑色。

我这样分配材料:

private func setupLightMeshes(_ scene: Entity) {
    let lightEntity = scene.findEntity(named: "LightWindow_Plane")!
    var lightMaterial = UnlitMaterial()
    
    lightMaterial.baseColor = try! MaterialColorParameter.texture(
    TextureResource.load(named: "light.png")) // this is 8bpc RGBA
    var modelComponent = lightEntity.components[ModelComponent] as! ModelComponent
    modelComponent = ModelComponent(mesh: modelComponent.mesh, materials: [lightMaterial])
    lightEntity.components.set(modelComponent)
}
Run Code Online (Sandbox Code Playgroud)

augmented-reality swift arkit realitykit

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

cakePHP hasOne为空

我是CakePHP的新手,我有一个关系问题.

我的模型看起来像这样:

<?
class Operator extends AppModel
{
    var $name = 'Operator';
    var $hasOne = array('Contact','Adress','Land');
}
?>
Run Code Online (Sandbox Code Playgroud)

和我的表看起来像这样:

CREATE TABLE `operators` (
  `id` varchar(45) NOT NULL,
  `name` int(11) NOT NULL,
  `adress_id` int(11) NOT NULL,
  `land_id` int(11) NOT NULL,
  `contact_id` int(11) NOT NULL,
  `operator_category_id` int(11) NOT NULL,
  `legal_form` varchar(45) DEFAULT NULL,
  `affidavit` varchar(45) DEFAULT NULL,
  `comment` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  KEY `operator_category_fk_idx` (`operator_category_id`),
  KEY `contact_id_idx` (`contact_id`),
  KEY `adress_id_idx` (`adress_id`),
  KEY `land_id_idx` (`land_id`),
  CONSTRAINT `adress_id` …
Run Code Online (Sandbox Code Playgroud)

mysql cakephp

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

检查 nullptr 100% 是否可以防止有关内存布局的段错误?

假设我正在迭代一个数组,但超出了这个数组,因为我的循环很愚蠢。
如果内存中的对象直接位于与该数组相同类型的该数组之后,
检查可以array[invalid_index] == nullptr
保护我吗?

检查索引(大小未知)是否对 C 数组有效的正确方法是什么?

c++ segmentation-fault memory-layout nullptr

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

仅更改成员的子类是否有效?

假设我有一个A具有 type 成员的类int。我有一个类B,它是A. B旨在将成员初始化为某种状态,没有其他目的。

#include <string>
#include <iostream>
struct A {
    int someInt;
    A() : someInt(33){}
};

struct B : public A {
    B() {
        someInt = 4;
    }
};
int main() {

    A a = A();
    A b = B();
    std::cout<< a.someInt << std::endl;
    std::cout << b.someInt << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

请注意我如何使用A b = B()对象切片应该发生的地方。但是,由于B没有向 中添加任何内容 A,它是否是使用A不同构造函数参数(或创建 的任何其他形式的实例A)的有效替代方法?

编辑:背景是我有一个具有一些复杂设置的类。将初始化放在单独的子类中比编写构造函数、工厂或构建器要容易得多。

c++ object-slicing

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

如何在定义之间添加空行?

我成功地将我的代码设置为 clang-format 格式,就像 iIwant 一样。然而,有一点让我很困扰:

我想要在结构/类/函数的定义之间以及函数的声明之间有一个空行。目前,在格式化时,clang-format 会删除空行,这使得所有内容都变得简洁。

这是我的文件:

---
AlignAfterOpenBracket: DontAlign
AlignTrailingComments: "true"
AllowAllArgumentsOnNextLine: "false"
AllowAllConstructorInitializersOnNextLine: "true"
AllowAllParametersOfDeclarationOnNextLine: "false"
AllowShortBlocksOnASingleLine: "false"
AllowShortCaseLabelsOnASingleLine: "false"
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: None
AllowShortLoopsOnASingleLine: "false"
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: "false"
AlwaysBreakTemplateDeclarations: "Yes"
BinPackArguments: "true"
BinPackParameters: "true"
BreakBeforeTernaryOperators: "false"
BreakConstructorInitializers: AfterColon
BreakInheritanceList: AfterColon
ColumnLimit: "170"
CompactNamespaces: "false"
ConstructorInitializerAllOnOneLineOrOnePerLine: "true"
IncludeBlocks: Merge
IndentCaseLabels: "true"
IndentPPDirectives: BeforeHash
IndentWidth: "4"
IndentWrappedFunctionNames: "false"
KeepEmptyLinesAtTheStartOfBlocks: "false"
Language: Cpp
MaxEmptyLinesToKeep: "0"
NamespaceIndentation: All
PointerAlignment: Left
SortIncludes: "true"
SortUsingDeclarations: "true"
SpaceAfterCStyleCast: "false"
SpaceAfterLogicalNot: …
Run Code Online (Sandbox Code Playgroud)

c++ clang-format

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

如何声明一个采用 T 的 output_iterator 的模板函数?

我想把对象放入一个容器中(哪个容器不固定)。

为此我想使用这个概念std::output_iterator

我如何定义一个以 astd::insert_iterator<std::vector<T>>和 a为例的函数std::insert_iterator<std::list<T>>

这个概念std::output_iterator需要两个模板参数:IT。所以我不确定如何声明这样的函数。

我可以像老派那样做<algorithm>并声明如下:

template<typename OutIter>
void foo(OutIter writer);
Run Code Online (Sandbox Code Playgroud)

但在我看来,这并不是那么有表现力。


更新:这是我基于@RemyLebeau 的回答的尝试:

#include <iterator>
#include <vector>
template<typename I, typename T, std::output_iterator<I,T> OutIter>
void foo(OutIter writer) {
    writer++ = T();
}

int main() {

    std::vector<int> ints;
    auto inserter = std::insert_iterator(ints,ints.end());
    foo(inserter);
}
Run Code Online (Sandbox Code Playgroud)

https://godbolt.org/z/afe9rz3c4

c++ c++-concepts

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

我可以安全地复制 vector&lt;array&gt; 吗?

我有一个vector<array<float,3>>用作渲染的 3D 坐标列表。
我可以简单地使用 memcpy withsizeof(array<float,3>) * vector.size()作为count参数吗?vector 的内存可以安全复制吗?

€dit:这是我的代码片段。我的问题现在措辞有点不正确。

vector<array<float,3>> vertexPositions;
//read VertexPositions, one line represents a vertex position and put them into vertexPositions with push_back();

D3D11_BUFFER_DESC bufferDesc;
//initialize Buffer Description;

D3D11_SUBRESOURCE_DATA bufferInitialData;
bufferInitialData.pSysMem = vertexPositions.data(); //pSysMem is a void* and expects a sequence of floats
Run Code Online (Sandbox Code Playgroud)

pSysMem只是一个void*,结构有一个额外的大小参数,我设置为sizeof(array<float,3>) * vector.size()

c++ arrays vector memcpy

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

使用 std::move 进行无限递归

我有一个模仿 std::vector 的类。为此,我想要一个函数 push_back 接受左值和右值。

    void push_back(const T& obj) {
        push_back(std::move(obj));
    }

    void push_back(T&& obj) {
        buffer[numElements] = obj;
        numElements = numElements+1>=Sz-1 ? Sz-1 : numElements+1;

    }
Run Code Online (Sandbox Code Playgroud)

但是,当我由于移动而传递左值时,此代码以无限递归结束,它调用了错误的函数(我希望const T& obj函数调用T&& obj重载)。

std::move 的文档说

它完全等同于 static_cast 到右值引用类型。

我错过了什么?

c++ move rvalue lvalue

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

使用传递的 lambda 调用 std::visit

我有一个包含variant.

我想为该结构编写一个成员函数,该函数应该根据当前持有的类型变体运行代码。

但是,我在编译时遇到问题。我不想使用更多的“模板恶作剧”,比如使用单独的结构来定义operator(T&),因为它会进一步污染语法。这是一个例子:

struct Data {

std::variant<int, double> var;


//Into this function,multiple lambdas should be passed for cases that the user wants to handle
template<typename ... Funcs>
void apply(Funcs&&... funcs) {
    std::visit(std::forward<Funcs>(funcs)...,var);
}
};
int main() {
    Data d;
    d.var = 4;
    //variant holds int and lambda provided that takes int&, execute it:
    d.apply([](int& i){
        std::cout << "I am Int Poggers" << std::endl;
    });
    d.var = 0.0;
    //variant holds double but no lambda passed that takes …
Run Code Online (Sandbox Code Playgroud)

c++ c++17 std-variant

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

模板参数与声明不兼容

我有一个函数应该生成随机整数或随机浮点值。为此我想使用概念。

由于整数和浮点数分别需要不同的分布,因此std::uniform_int_distributionstd::uniform_real_distribution使用单独的结构来选择正确的类型 - 也具有概念重载。“选择器”看起来像这样:

template<std::integral I>
struct distribution_selector {
    using type = std::uniform_int_distribution<I>;
};

template<std::floating_point F>
struct distribution_selector {
    using type = std::uniform_real_distribution<F>;
};
Run Code Online (Sandbox Code Playgroud)

您会看到,我过去using常常选择不同的类型,具体取决于我使用的是整数类型还是浮点类型。

现在我的实际功能:

template<typename T = float, random_mode quality = random_mode::high_quality>
requires std::integral<T> || std::floating_point<T>
constexpr inline T rand(random& r, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max()) {
    using distribution = distribution_selector<T>::type;
    if constexpr(quality == random_mode::low_quality) {
        return distribution<T>(min, max)(r.low_quality_engine);
    } else {
        return distribution<T>(min, max)(r.high_quality_engine);
    }
}
Run Code Online (Sandbox Code Playgroud)

演示

我收到以下错误 …

c++ c++-concepts c++20

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

将定义嵌入到字符串中

我有一个预处理器定义,它应该确定数组的大小。
该常量还应该传递给 HLSL 着色器。
为此,我需要将其作为字符串传递。
有没有办法将预处理器定义嵌入为字符串?

#include <iostream>
#ifndef SIZE
#define SIZE 16
#endif
int main() {
    const int arr[SIZE] = {}; // array size is 16 :)
    const char* str = "SIZE"; // literal is "SIZE" and not "16" :(
    std::cout << str << std::endl; // should print "16"
    std::cout << SIZE << std::endl; // prints 16, but is not a string
}
Run Code Online (Sandbox Code Playgroud)

c++ string c-preprocessor

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