我在 cpp 参考网站上读到了有关缩小转换的内容。我有点理解它,但我不明白的是为什么错误只出现在第一行。
long double ld = 3.1415926536;
int a{ld}, b = {ld}; // error: narrowing conversion required
int c(ld), d = ld; // ok: but value will be truncated
Run Code Online (Sandbox Code Playgroud)
为什么错误只出现在第一行而不是第二行?
我遇到了该push_back函数的以下语法。Vertex只是一个包含三个浮点数 x、y 和 z 的结构。第二行看起来就像会写它。但是第一行对我来说看起来很奇怪。在解释这一点的视频中,据说这是通过成员初始值设定项列表完成的,但它看起来更像是隐式转换。我只是对那里的大括号感到困惑。谁能解释为什么这种语法有效?
std::vector<Vertex> vertices;
vertices.push_back({ 1, 2, 3 });
vertices.push_back(Vertex(1, 2, 3));
Run Code Online (Sandbox Code Playgroud) 我错过了一些东西std::make_shared。它不能解决 a 的类型std::initializer_list,还是我做错了什么?
#include <vector>
#include <memory>
class A {};
int main()
{
A a;
std::vector<A> veca{A(), A{}, a}; // this works ofc
std::vector<A> vecb({A(), A{}, a}); // this too
std::make_shared<std::vector<A>>(vecb); // and this, ofc
std::make_shared<std::vector<A>>({a}); // what's wrong here?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
main.cpp:21:41: error: too many arguments to function ‘std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = std::vector; _Args = {}]’
std::make_shared<std::vector<A>>({a});
^
In file included from /usr/include/c++/6/memory:82:0,
from main.cpp:10:
/usr/include/c++/6/bits/shared_ptr.h:632:5: note: declared here
make_shared(_Args&&... __args) …Run Code Online (Sandbox Code Playgroud) 这段代码:
#include <stdio.h>
struct
{
int i;
const char* str;
} ar[] = {
1,"asd", //should be {1, "asd"},
2, "qwe", //should be {2, "qwe"},
3, "poi" //should be {3,"poi"}
};
int main()
{
printf("%s\n", ar[2].str);
}
Run Code Online (Sandbox Code Playgroud)
工作得很好,即使数组的每个元素都ar应该用大括号括起来(至少我希望如此)。为什么这是可能的?
考虑以下代码:
struct S
{
S(int, double) {}
explicit S(const S&) {}
explicit S(S&&) {}
};
void i_take_an_S(S s) {}
S i_return_an_S() { return S{ 4, 2.0 }; }
int main()
{
i_take_an_S(i_return_an_S());
}
Run Code Online (Sandbox Code Playgroud)
使用“-std=c++17”标志,g++ 和 clang++ 都可以很好地编译此代码。然而,MSVC(带有 /std:c++17 标志)报告
“错误 C2664:'void i_take_an_S(S)':无法将参数 1 从 'S' 转换为 'S'”
作为编译错误,带有附加注释
“结构‘S’的构造函数被声明为‘显式’”。
根据C++17的初始化规则(第3点的解释)S,不应考虑使用复制构造函数来S初始化i_take_an_S;S(int, double)应该通过直接列表初始化选择精确匹配。
这可能是 MSVC 中的一个错误吗?
这是我的代码:
#include <string>
struct A
{
int a;
std::string sa;
};
int main()
{
A arr[3]{};
}
Run Code Online (Sandbox Code Playgroud)
当我用gcc 4.8.2(在Ubuntu 14.04上)使用-std=gnu++11选项编译它时会出现以下错误:
example.cpp: In function ‘int main()’:
example.cpp:11:14: internal compiler error: in gimplify_init_constructor, at gimplify.c:4271
A arr[3]{};
^
Run Code Online (Sandbox Code Playgroud)
为什么会抛出内部编译器错误?这是编译器错误吗?
我有以下代码:
bool c (a == b);
Run Code Online (Sandbox Code Playgroud)
和
bool c {a == b};
Run Code Online (Sandbox Code Playgroud)
其中a和b是一些相同类型的变量.
我想知道,上述两个初始化有什么不同,哪个应该在什么条件下首选?任何形式的帮助将不胜感激.
#include <iostream>
struct int_wrapper
{
int i;
int_wrapper(int i = 0) : i(i) {}
};
struct A
{
int_wrapper i;
int_wrapper j;
A(int_wrapper i = 0, int_wrapper j = 0) : i(i), j(j) {}
};
int main()
{
A a;
a = {3};
// error: no match for ‘operator=’ (operand types are ‘A’ and ‘int’)
// a = 3;
std::cout << a.i.i << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道在进行隐式转换时不允许多个用户转换,但是,为什么使用brace-init-list双重用户转换有效?
考虑以下代码,一个简单的类,其构造函数采用带有默认值的参数.
// Version 1
template <class T>
struct object1 {
using type = T;
constexpr object1(const type& val = type()): value(val) {}
type value;
};
// Version 2
template <class T>
struct object2 {
using type = T;
constexpr object2(const type& val = {}): value(val) {}
type value;
};
// Main
int main(int argc, char* argv[]) {
using type = /* Something */;
object1<type> x1;
object2<type> x2;
auto value1 = x1.value;
auto value2 = x2.value;
// Is there certain types …Run Code Online (Sandbox Code Playgroud) c++ default-constructor default-arguments c++11 list-initialization
我正在尝试std::unordered_map使用构造函数初始化一个构造函数,该构造函数通过初始化列表和初始存储桶数量接受数据。
出于某种原因,如果将其放入main,则该构造函数会起作用,但是将其放入类标头时会出现语法错误。
具体来说,标题为momo.h:
#pragma once
#include <unordered_map>
namespace std
{
template <>
struct hash<std::pair<uint16_t, uint16_t>>
{
std::size_t operator()(const std::pair<uint16_t, uint16_t>& k) const
{
return (std::hash<long>()((((long)k.first) << 16) + (long)k.second));
}
};
}
class test
{
std::unordered_map<std::pair<uint16_t, uint16_t>, uint16_t> m_Z(
{ /* Fails here: syntax error: missing ')' before '{' */
{std::pair{ 1, 2 }, 3},
{std::pair{ 4, 5 }, 6}
}, 128);
};
Run Code Online (Sandbox Code Playgroud)
而如果我将标头中的定义删除为main:
#include "Momo.h"
int main()
{
test X;
std::unordered_map<std::pair<uint16_t, …Run Code Online (Sandbox Code Playgroud) c++ ×9
c++11 ×5
curly-braces ×2
c ×1
c++17 ×1
constructor ×1
gcc ×1
linux ×1
make-shared ×1
prvalue ×1
stdvector ×1
vector ×1