std::unique_ptr 支持数组,例如:
std::unique_ptr<int[]> p(new int[10]);
Run Code Online (Sandbox Code Playgroud)
但它需要吗?可能使用std::vector或更方便std::array.
你觉得这个结构有用吗?
使用C++ 17,我们得到内联变量.
其中一个用途是在类中定义常量字段.
那么这两个常量定义之间的区别是什么:
class MyClass {
static constexpr int myFirstVar = 10;
static const inline int mySecondVar = 100;
};
Run Code Online (Sandbox Code Playgroud)
当然constexpr是myFirstVar隐式内联.
什么是更好的选择,使用constexpr或inline?
注意:当你不需要constness时,那就inline更容易了.随着constexpr你没有这样的选择.
请参阅以下返回可选项的示例UserName- 可移动/可复制类.
std::optional<UserName> CreateUser()
{
UserName u;
return {u}; // this one will cause a copy of UserName
return u; // this one moves UserName
}
int main()
{
auto d = CreateUser();
}
Run Code Online (Sandbox Code Playgroud)
为什么要return {u}复制和return u移动?
这是相关的coliru样本:http://coliru.stacked-crooked.com/a/6bf853750b38d110
另一个案例(感谢@Slava的评论):
std::unique_ptr<int> foo()
{
std::unique_ptr<int> p;
return {p}; // uses copy of unique_ptr and so it breaks...
}
Run Code Online (Sandbox Code Playgroud) 在C++ 14中,我们获得了升级版本的constexpr含义,现在可以使用循环,if语句和开关.像C++ 11一样,递归已经成为可能.
我知道constexpr函数/代码应该很简单,但问题仍然存在:如何有效地调试它?
即使在" The C++ Programming Language,4th Edition "中也有一句话,调试可能很难.
我对Visual Studio允许将动态值保存到项目的不同方式以及如何使用它们感到困惑.
我知道如果我需要在我的应用程序中包含像图像或声音文件这样的二进制信息,我需要将其添加到资源文件中.但是,如果我将类似文件路径的内容保存为字符串,为什么我应该在资源文件中使用或不使用应用程序settings(app.config)文件或用户设置(myapp.dll.config)文件中的字符串?
.net settings resources application-settings visual-studio-2008
ARB_texture_storage被引入OpenGL 4.2核心.
你能解释一下纹理对象的不变性意味着什么吗?
为什么从以前的纹理使用情况来看更好,这个功能的缺点是什么?
我知道我可以阅读这个扩展的规范(我做了:)),但我想看一些例子或其他解释.
使用C++ 17,我们可以获得类模板的模板类型推导.所以许多make功能可能会过时.
如何make_unique与make_shared?
所以我们可以写
unique_ptr myPtr(new MyType());
// vs
auto myPtr = make_unique<MyType>();
Run Code Online (Sandbox Code Playgroud)
那么我们可以忘记那些功能吗?
使用C++ 17,我们得到了std::optional一个用于表示可空类型的有用包装器.
我可以用它来处理错误吗?这样做非常有吸引力:
optional<int> Compute()
{
//... compute something valid...
return std::nullopt; // error!
}
Run Code Online (Sandbox Code Playgroud)
这是一个不错的选择吗?返回空指针是不是一样?
人们报告的问题是,如果发生错误,您将丢失一条消息.所以返回一些状态代码可能会更好.
其他选择:
std::variant<Value, errorCode>std::pair<Value, errorCodestd::chrono::parse并std::chrono::from_stream允许我们解析许多不同的日期/时间格式。
但我最近得到了这样的字符串:
2022-07-10 22:00 GMT+2- 我不确定应该使用什么格式。
我尝试过:
%F %T GMT%z- 失败
%F %T GMT%Z- 失败
%F %T %Z- 失败
或者也许我只需要解析时间/日期,然后手动获取区域?
我希望能够通过着色器中的实际名称来设置制服
myProgram.uniform3fv("uniformVector", 0.0f, 0.1f, 1.0f);
Run Code Online (Sandbox Code Playgroud)
我是否必须以某种形式的地图缓存位置?
std::map<std::string, unsigned int> // or unordered_map
Run Code Online (Sandbox Code Playgroud)
或者OpenGL(在桌面上)无论如何缓存它,所以我不会有任何性能差异?