为什么make_unique调用编译?make_unqiue不要求其模板参数是完整类型吗?
struct F;
int main()
{
std::make_unique<F>();
}
struct F {};
Run Code Online (Sandbox Code Playgroud)
在从orignated问题我的"问题"与我PIMPL实现:
我确实理解为什么析构函数必须在用户声明并在实现类(PIMPL)的cpp文件中定义.
但是移动包含pimpl的类的构造函数仍会编译.
class Object
{};
class CachedObjectFactory
{
public:
CachedObjectFactory();
~CachedObjectFactory();
std::shared_ptr<Object> create(int id) const;
private:
struct CacheImpl;
std::unique_ptr<CacheImpl> pImpl;
};
Run Code Online (Sandbox Code Playgroud)
现在cpp文件:
// constructor with make_unique on incompete type ?
CachedObjectFactory::CachedObjectFactory()
: pImpl(std::make_unique<CacheImpl>())
{}
struct CachedObjectFactory::CacheImpl
{
std::map<int, std::shared_ptr<Object>> idToObjects;
};
//deferred destructor
CachedObjectFactory::~CachedObjectFactory() = default;
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么这个编译?为什么建筑和破坏之间存在差异?如果析构函数的实例化和default_deleter的实例化是一个问题,为什么make_unique的实例化不是问题?
我有以下(设计)代码,其中我有一个打印机类,其中包含一个打印函数和一个处理字符串然后调用回调函数到print函数的工作类:
#include <functional>
#include <iostream>
using callback_fn = std::function<bool(std::string)>;
class printer
{
public:
bool print(std::string data)
{
std::cout << data << std::endl;
return true;
}
};
class worker
{
public:
callback_fn m_callback;
void set_callback(callback_fn callback)
{
m_callback = std::move(callback); // <-- 1. callback is a temp, so what does std::move do here?
}
void process_data(std::string data)
{
if (!m_callback(data)) { /* do error handling */ }
}
};
int main() {
printer p;
worker w;
w.set_callback( std::move([&](std::string s){ return p.print(s); …Run Code Online (Sandbox Code Playgroud) 看起来,Concepts可以使用基于SFINAE的技术完成您所做的一切,但效果要好得多.优点列表包括提高过载分辨率的可读性,并使编译器诊断功能大大降低.
一个选项,因为所有的这些概念 "的特点是已经在新草案的一部分,是一些或所有这些SFINAE相关辅助模板被宣布弃用.
我主要担心的是功能碎片.
我的问题是这样的弃用提案是否已经提交?
该页面std::basic_filebuf::sync()说
sync()或其等价物由 ... 隐式调用seekoff(),并且seekpos()...
但我似乎在语言规范中找不到任何提及这一点的地方,无论是这里还是这里。
我读过filebuf这两者的规范部分seekoff(),甚至seekpos()不清楚它们是否有效地完成了它们要做的事情sync()(以及它们要做的其他事情),更不用说称之为它或它的等效项了。
cppreference上的这段措辞是否具有误导性?
在尝试深入研究这个问题背后的机制之后,我仍然不明白为什么下面代码中的第三行只生成警告,而第二行是错误.
int main()
{
const char* const& a = "bla"; // Valid code
const char*& a2 = "bla"; // Invalid code
char* const& a3 = "bla"; // Should be invalid but settles for a warning
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道的是,虽然参考初始化被转换字符串文字的指针参考那么它不应该被丢弃任何cv修饰符对象具有,和作为转换类型const char* const(从字符串文字转换"bla",即,const char[4])这似乎是与第二行相同的情况.唯一的区别是const被删除属于C字符串本身而不是指针.
在没有指定任何额外的一致性标志的情况下在GCC 8.2和Clang 6.0.0上重现.
gcc的输出:
<source>:4:23: error: cannot bind non-const lvalue reference of type 'const char*&' to an rvalue of type 'const …Run Code Online (Sandbox Code Playgroud) 我知道我可以std::future通过以下方式检查状态:
my_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready
但是根据cppreference.com, std::future::wait_for在某些情况下可能会阻止:
由于调度或资源争用延迟,此功能可能阻塞的时间超过timeout_duration。
还是timeout_duration0 还是这样吗?如果是这样,是否还有另一种以保证免等待的方式查询状态的方法?
我正在使用某些C ++,并且我的代码中有错别字导致了这种情况。将来,我宁愿Visual Studio在直接从Visual Studio 2017(社区版)以调试模式运行时使用实际表达式直接在错误代码上中断,而不是向我显示此烦人的提示。
在某处有此设置吗?
这里我使用两种语法将值传递给构造函数: -
class A
{
public:
int x,y;
A(int a,int b) : x(a),y(b){}
void show()
{
cout<<x<<" "<<y<<endl;
}
};
int main()
{
A obj1={5,6};//first method
A obj2(9,10);//second method
obj1.show();
obj2.show();
}
Run Code Online (Sandbox Code Playgroud)
并且两者都工作正常,但是当我删除构造函数本身时,第一种方法也正常工作.请解释一下.
我正在使用C++在Visual Studio 2017中创建DirectX11应用程序,我需要在我的"GeometryGenerator.h"头文件中声明一个数据结构.
问题是当我尝试使用类型:我的头文件中的XMFLOAT3时,当我尝试运行项目时,我从Visual Studio收到错误并给我这样的消息:
“C4430: missing type specifier - int assumed”
Run Code Online (Sandbox Code Playgroud)
在我声明XMFLOAT3类型的变量的行中
这是我的代码:
#pragma once
#include "..\Common\DeviceResources.h"
#include "ShaderStructures.h"
#include "..\Common\StepTimer.h"
namespace DirectX11Engine
{
class GeometryGenerator {
public:
struct Vertex
{
Vertex() {}
Vertex(const XMFLOAT3& p, const XMFLOAT3& n, const XMFLOAT3& t, const XMFLOAT2& uv)
: Position(p), Normal(n), TangentU(t), TexC(uv) {}
Vertex(
float px, float py, float pz,
float nx, float ny, float nz,
float tx, float ty, float tz,
float u, float v)
: Position(px, py, pz), Normal(nx, …Run Code Online (Sandbox Code Playgroud) c++ ×9
c++11 ×2
c++-concepts ×1
c++20 ×1
class ×1
constructor ×1
directx-11 ×1
filebuf ×1
header ×1
namespaces ×1
oop ×1
pimpl-idiom ×1
std-future ×1
stdmove ×1
templates ×1
unique-ptr ×1