假设我有这个功能:
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
我正在使用CAMetalLayer
绘制到 UIView 上。编译器会抛出一个CAMetalLayer
仅在 iOS 13.0 上可用的警告。然而
,文档说iOS 8.0+
。
我可以忽略这个警告吗?我这里只有 iOS 13.0 设备,我们也想支持 12 和 11。
纹理是否可以具有 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) 我是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) 假设我正在迭代一个数组,但超出了这个数组,因为我的循环很愚蠢。
如果内存中的对象直接位于与该数组相同类型的该数组之后,
检查可以array[invalid_index] == nullptr
保护我吗?
检查索引(大小未知)是否对 C 数组有效的正确方法是什么?
假设我有一个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
)的有效替代方法?
编辑:背景是我有一个具有一些复杂设置的类。将初始化放在单独的子类中比编写构造函数、工厂或构建器要容易得多。
我成功地将我的代码设置为 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) 我想把对象放入一个容器中(哪个容器不固定)。
为此我想使用这个概念std::output_iterator
。
我如何定义一个以 astd::insert_iterator<std::vector<T>>
和 a为例的函数std::insert_iterator<std::list<T>>
?
这个概念std::output_iterator
需要两个模板参数:I
和T
。所以我不确定如何声明这样的函数。
我可以像老派那样做<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)
我有一个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()
我有一个模仿 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 到右值引用类型。
我错过了什么?
我有一个包含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) 我有一个函数应该生成随机整数或随机浮点值。为此我想使用概念。
由于整数和浮点数分别需要不同的分布,因此std::uniform_int_distribution
我std::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)
我收到以下错误 …
我有一个预处理器定义,它应该确定数组的大小。
该常量还应该传递给 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)