在C#7中是否可以在字典中的foreach循环中使用解构?像这样的东西:
var dic = new Dictionary<string, int>{ ["Bob"] = 32, ["Alice"] = 17 };
foreach (var (name, age) in dic)
{
Console.WriteLine($"{name} is {age} years old.");
}
Run Code Online (Sandbox Code Playgroud)
它似乎不适用于Visual Studio 2017 RC4和.NET Framework 4.6.2:
错误CS1061:'KeyValuePair'不包含'Deconstruct'的定义,并且没有扩展方法'Deconstruct'可以找到接受类型'KeyValuePair'的第一个参数(你是否缺少using指令或汇编引用?)
When I resize my window, I need to resize my textures that are attached to my framebuffer. I tried calling glTexStorage2D again, with different size parameters. However that does not work.
How can I resize the textures attached to my framebuffer? (Including the depth attachment)
EDIT
Code I tried:
glBindTexture(m_target, m_name);
glTexStorage2D(m_target, 1, m_format, m_width, m_height);
glBindTexture(m_target, 0);
Run Code Online (Sandbox Code Playgroud)
where m_name, m_target and m_format are saved from the original texture and m_width and m_height are the new dimensions.
EDIT2
Please tell …
struct Object {
Object() { cout << "constructor\n"; }
Object(const Object &) { cout << "copy constructor\n"; }
Object(Object &&) { cout << "move constructor\n"; }
};
int main() {
vector<Object> v;
v.reserve(10);
v.emplace_back(Object{});
}
Run Code Online (Sandbox Code Playgroud)
这给了我以下输出:
构造函数
移动构造函数
为什么?我认为emplace_back确实创建了Object,因此不必调用复制或移动构造函数.
从描述:
元素就地构造,即不执行复制或移动操作.
编辑:啊,好吧,似乎我从根本上误解了emplace_back().您不必将Object作为参数,因为它会自动为您创建.您只需将Object-constructor的参数提供给emplace_back().
所以,如果我有一个像这样的新构造函数:
Object(int) { cout << "int constructor\n"; }
Run Code Online (Sandbox Code Playgroud)
我会像这样调用emplace_back:
v.emplace_back(42);
Run Code Online (Sandbox Code Playgroud)
而不是这个:
v.emplace_back(Object(42));
Run Code Online (Sandbox Code Playgroud)
现在有道理,非常感谢!
编辑2:我希望我能接受你所有的答案!:-P
我最近阅读了有关复制和交换的内容,现在我正在尝试在基类和派生类中实现ctors.我的base和派生类中都有四个构造函数,但是我不确定如何实现派生类的赋值运算符.
explicit Base(int i) : m_i{i} {}
Base(const Base & other) : m_i{other.m_i}
Base(Base && other) : Base(0) { swap(*this, other); }
Base & operator=(Base other) { swap(*this, other); return *this; }
friend void swap(Base & a, Base & b) noexcept {
using std::swap;
swap(a.m_i, b.m_i);
}
explicit Derived(int j) : Base(42), m_j(j) {}
Derived(const Derived & other) : Derived(other.m_j) {}
Derived(Derived && other) : Derived(other.m_j) { swap(*this, other); }
Derived & operator=(Derived other) { /*???*/ }
friend …Run Code Online (Sandbox Code Playgroud) 如何使用Clang 3.5在QtCreator 3.3中启用C++ 14支持?我添加了一个Clang工具包,我已添加CONFIG += c++14到我的项目文件中.但是当使用例如返回类型推导时,我得到以下错误:
错误:'auto'返回没有尾随返回类型; 推导的返回类型是C++ 1y扩展
是否可以将构造函数与固定参数和构造函数模板混合使用?
我的代码:
#include <iostream>
class Test {
public:
Test(std::string, int, float) {
std::cout << "normal constructor!" << std::endl;
}
template<typename ... Tn>
Test(Tn ... args) {
std::cout << "template constructor!" << std::endl;
}
};
int main() {
Test t("Hello World!", 42, 0.07f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这给了我"模板构造函数!".有没有办法,我的普通构造函数被调用?
我如何为qTreeView的qTreeWidget实现这个代码?
for (const auto & i : names) {
QTreeWidgetItem * item = new QTreeWidgetItem(ui->treeWidget);
item->setText(0, QString::fromStdString(i));
ui->treeWidget->addTopLevelItem(item);
const std::unordered_map<std::string, double> map = m_reader.getMapFromEntry(i);
for (const auto & j : map) {
QTreeWidgetItem * item2 = new QTreeWidgetItem();
item2->setText(0,QString::fromStdString(j.first));
item2->setText(1,QString::number(j.second));
item->addChild(item2);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个模型和一个treeView,像这样:
m_model = new QStandardItemModel(m_reader.getAllNames().size(),2,this);
ui->treeView->setModel(m_model);
Run Code Online (Sandbox Code Playgroud)
我尝试过这个,但只显示一列:
QStandardItem * parentItem = m_model->invisibleRootItem();
for (const auto & i : names) {
QStandardItem * item = new QStandardItem(QString::fromStdString(i));
parentItem->appendRow(item);
const std::unordered_map<std::string, double> map = m_reader.getMapFromEntry(i);
for (const auto & …Run Code Online (Sandbox Code Playgroud) 我有一个按钮样式.根据Button是否启用,我想更改背景.这就是它的样子:
<Style x:Key="MyButtonStyle" TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled, PresentationTraceSources.TraceLevel=High}" Value="False">
<Setter Property="Background" Value="Purple"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled, PresentationTraceSources.TraceLevel=High}" Value="True">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
这只是一个基本的例子.实际上我需要一个MultiDataTrigger,但它甚至不能使用常规的DataTrigger.我只看到一个灰色按钮.
这是跟踪:
System.Windows.Data警告:56:为绑定创建BindingExpression(hash = 31767240)(hash = 6303779)
System.Windows.Data警告:58:路径:'IsEnabled'System.Windows.Data
警告:60:BindingExpression(hash = 31767240):默认模式已解析为OneWay
System.Windows.Data警告:61:BindingExpression(hash = 31767240):解析为PropertyChanged的默认更新触发器
System.Windows.Data警告:62:BindingExpression(hash = 31767240):附加到系统. Windows.Controls.Button.NoTarget(hash = 24311680)
System.Windows.Data警告:66:BindingExpression(hash = 31767240):RelativeSource(FindAncestor)需要树上下文
System.Windows.Data警告:65:BindingExpression(hash = 31767240) :Resolve source deferred
System.Windows.Data警告:67:BindingExpression(hash = 31767240):解析源
System.Windows.Data警告:70:BindingExpression(hash = 31767240):找到数据上下文元素:(OK)
System.Windows.数据警告:73:查找类型Button的祖先:查询网格(哈希= 35 377238)
System.Windows.Data警告:73:查找类型Button的祖先:查询ContentPresenter(hash = 51189900)
System.Windows.Data警告:73:查找类型Button的祖先:查询边框(hash = 48541090)
System.Windows.数据警告:73:查找类型Button的祖先:查询StartStopControl(hash = 22721178)
System.Windows.Data警告:73:查找类型Button的祖先:查询网格(哈希= 32321338) …
我有一个类Input,它有默认的移动/复制构造函数.
Input(const Input &) = default;
Input(Input &&) = default;
Run Code Online (Sandbox Code Playgroud)
但是,以下断言失败了.
static_assert(std::is_copy_constructible<Input>(), "Input is not copy-constructible");
static_assert(std::is_move_constructible<Input>(), "Input is not move-constructible");
Run Code Online (Sandbox Code Playgroud)
这是为什么?
这是一个完整的例子:
#include <type_traits>
class A {
public:
A(const A &) = default;
static_assert(std::is_copy_constructible<A>(), "");
};
int main() {
// your code goes here
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是这个问题的后续。
我已经像这样设置了复制构造函数:
glGenBuffers(1, &m_vertexBuffer);
glGenBuffers(1, &m_colorBuffer);
glGenBuffers(1, &m_indexBuffer);
glGenVertexArrays(1, &m_vertexArray);
glBindVertexArray(m_vertexArray);
GLint size = 0;
glBindBuffer(GL_COPY_READ_BUFFER, other.m_indexBuffer);
glGetBufferParameteriv(GL_COPY_READ_BUFFER, GL_BUFFER_SIZE, &size);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, nullptr, GL_STATIC_DRAW);
glCopyBufferSubData(other.m_indexBuffer, m_indexBuffer, 0, 0, size);
glBindBuffer(GL_COPY_READ_BUFFER, other.m_vertexBuffer);
glGetBufferParameteriv(GL_COPY_READ_BUFFER, GL_BUFFER_SIZE, &size);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, size, nullptr, GL_STATIC_DRAW);
glCopyBufferSubData(other.m_vertexBuffer, m_vertexBuffer, 0, 0, size);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *) 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_COPY_READ_BUFFER, other.m_colorBuffer);
glGetBufferParameteriv(GL_COPY_READ_BUFFER, GL_BUFFER_SIZE, &size);
glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
glBufferData(GL_ARRAY_BUFFER, size, nullptr, GL_STATIC_DRAW);
glCopyBufferSubData(other.m_colorBuffer, m_colorBuffer, 0, 0, size);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void *) …Run Code Online (Sandbox Code Playgroud) c++ ×6
c++11 ×2
c++14 ×2
opengl ×2
base-class ×1
c# ×1
c#-7.0 ×1
clang ×1
constructor ×1
data-binding ×1
datatrigger ×1
fbo ×1
qt ×1
qt-creator ×1
qtreeview ×1
vector ×1
wpf ×1