我试图使用a std::optional来实例化一个对象(之前无效).我发现了一个烦人的情况,我不知道如何优雅地解决这个问题.
我有以下数据结构:
struct Foo {
int foo1;
float foo2;
};
Run Code Online (Sandbox Code Playgroud)
作为会员std::optional<Foo> foo_.
在一个功能
void Bar::bar(int const value1, float const value2) {
foo_.emplace(value1, value2);
}
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,这无法编译(在GCC 7.1中),因为它试图调用Foowith 的构造函数int const&, float const&.现在天真的我试图专注emplace于:
foo_.emplace<int, float>(value1, value2);
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为它试图使用initializer_list那么.
所以我的问题是如何优雅地召唤出来?
我只是想学习新的c ++ 17更改相关的"直接列表初始化的自动规则"
很少有stackoverflow问题线程有答案,如不安全的事情 为什么直接列表初始化与自动被认为是坏或不首选?
尝试了一个选定的答案来理解
#include <typeinfo>
#include <iostream>
struct Foo{};
void eatFoo (const Foo& f){}
int main() {
Foo a;
auto b{a};
eatFoo(b);
std::cout << "a " << typeid(a).name() << '\n';
std::cout << "b " << typeid(b).name() << '\n';
}
Run Code Online (Sandbox Code Playgroud)
但令我惊讶的是它编译时没有任何警告或编译错误输出
a 3Foo
b 3Foo
Program ended with exit code: 0
Run Code Online (Sandbox Code Playgroud)
这是否意味着现在使用auto进行直接初始化是安全的
比如这样
auto x { 1 };
Run Code Online (Sandbox Code Playgroud) 我刚刚安装了最后一个版本的Visual Studio,我有这个元组的deque:
using InstancesOfOneObject = std::tuple<DrawCmd, std::deque<bool>, std::deque<glm::mat4>>;
std::deque<InstancesOfOneObject> mInstancesByObject;
Run Code Online (Sandbox Code Playgroud)
之后,我想用一个远程循环来遍历这个双端队列:
for (const auto &[cmd, validites, matrices] : mInstancesByObject)
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,但是:
for (const auto &instance : mInstancesByObject) {
const auto &[cmd, validities, matrices] = instance;
Run Code Online (Sandbox Code Playgroud)
效果很好.
这是正常的吗?有没有办法使用接近第一个想法的东西?
我试图使用自己的分配器来测量C++中的内存使用情况std::set.不幸的是,我在链接时遇到错误.为了简化问题,我有以下程序:
#include<set>
#include<vector>
#include<memory>
//using Container = std::vector<int, std::allocator<int>>;
using Container = std::set<int, std::allocator<int>>;
int main() {
Container container;
container.push_back(4711);
container.insert(4711);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果可以在wandbox https://wandbox.org/permlink/R5WcgSvSWiqstYxL#wandbox-resultwindow-code-body-1中找到
我试过gcc 6.3.0,gcc 7.1.0,clang 4.0.0和clang 6.0.0HEAD.在所有情况下,我使用a时都会出错std::set,但是当我使用a时却没有std::vector.
如何声明我的设置使用分配器?
我想使用C++ 17,但C++ 14中的答案也很好.
当函数需要返回两个参数时,可以使用std :: pair编写它:
std::pair<int, int> f()
{return std::make_pair(1,2);}
Run Code Online (Sandbox Code Playgroud)
如果你想使用它,你可以这样写:
int one, two;
std::tie(one, two) = f();
Run Code Online (Sandbox Code Playgroud)
这种方法的问题是你需要定义'one'和'two'然后将它们分配给f()的返回值.如果我们能写出类似的东西会更好
auto {one, two} = f();
Run Code Online (Sandbox Code Playgroud)
我看了一个演讲(我不记得哪一个,对不起),演讲者说C++标准的人试图做那样的事情.我认为这个讲座是从2年前开始的.有谁知道现在(几乎是c ++ 17)你能做到或类似吗?
在代码块中构建或编译器选项中没有C++ 17选项,只有C++ 14如何在代码块中启用它,以便编码工具和编译器都支持它?
为什么this在静态成员函数中不允许未评估的上下文?
struct A
{
void f() {}
static void callback(void * self) // passed to C function
{
static_cast< decltype(this) >(self)->f();
}
};
Run Code Online (Sandbox Code Playgroud)
此代码给出错误:
错误:'this'不适用于静态成员函数
Run Code Online (Sandbox Code Playgroud)static_cast< decltype(this) >(self)->f(); ^~~~
decltype(this)为了简洁需要(有时它会短得多VeryVeryLongClassName *),另一个优点是意图更清晰.
标准说什么this在静态成员函数中使用未评估的上下文?
我constexpr在C++ 17中使用此参考链接阅读了该文章.
然后,我制作了C++程序进行测试constexpr:
#include <iostream>
int i = 10;
int func()
{
if constexpr (i == 0)
return 0;
else if (i > 0)
return i;
else
return -1;
}
int main()
{
int ret = func();
std::cout<<"Ret : "<<ret<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是,编译器会出错:
main.cpp: In function 'int func()':
main.cpp:8:25: error: the value of 'i' is not usable in a constant expression
if constexpr (i == 0)
^
main.cpp:4:5: note: 'int i' is not const
int …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的代码中使用一些c ++ 17功能,例如结构化绑定,但编译器一直给我错误,我不确定是不是因为我做错了或者我没有设置c + +17适合在VS17中工作.我正在尝试编译的简单代码是:
#include <iostream>
struct S
{
int i = 0;
float f = 32.0f;
};
int main()
{
S s;
auto [i, f] = s();
std::cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
从理解这个文章,这是我会怎么用新的C++语法17返回多个值.但是,我一直收到这些错误:
c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2059: syntax error: 'empty declaration'
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2143: syntax error: missing ';' before '['
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): warning C4467: usage of ATL attributes is deprecated
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2337: 'i': attribute not found
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error …Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么这个静态断言是错误的吗?如何重新定义is_empty以便它以我想要的方式工作(不改变语法)?默认情况下我不想评估为false的包类型(例如,is_empty<int>::value应为false).
#include <type_traits>
template <typename Pack> struct is_empty : std::false_type {};
template <typename T, template <T...> class Z, T... Is>
struct is_empty<Z<Is...>> : std::true_type {};
template <typename T, template <T...> class Z, T First, T... Rest>
struct is_empty<Z<First, Rest...>> : std::false_type {};
template <int...> struct Z;
int main() {
static_assert(is_empty<Z<>>::value);
}
Run Code Online (Sandbox Code Playgroud) c++ templates template-meta-programming variadic-templates c++17