打字
指针别名的一种形式,其中两个指针指向内存中的相同位置,但将该位置表示为不同类型.编译器会将"双关语"视为不相关的指针.类型惩罚有可能导致通过两个指针访问的任何数据的依赖性问题.
文章试图说什么?
有人会举一个"可修改的右值"的例子吗?我的理解是rvalue出现在表达式"="的右侧.我测试了以下示例,但我不确定它是否解释了"可修改的rvalue"
int i=1
int &j = i;
j=2; //cout: i == 2,
Run Code Online (Sandbox Code Playgroud) 考虑这个非常简单的代码:
#include <memory>
class Foo
{
public:
Foo() {};
};
class Bar
{
public:
Bar( const std::shared_ptr<Foo>& foo ) {}
};
int main()
{
Foo* foo = new Foo;
Bar bar( std::shared_ptr<Foo>( foo ) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么Visual Studio会报告
warning C4930: 'Bar bar(std::shared_ptr<Foo>)': prototyped function not called (was a variable definition intended?)
Run Code Online (Sandbox Code Playgroud)
并且没有bar创建对象......如何将此行Bar bar( std::shared_ptr<Foo>( foo ) );解释为函数定义?
我检查了类型名称与new之后的括号吗?还有C++:警告:C4930:未调用原型函数(是一个变量定义?),但我觉得我的问题不同,因为我没有使用语法Foo()也没有Bar().
编辑:请注意,它成功编译:
Foo* foo = new Foo;
std::shared_ptr<Foo> fooPtr( …Run Code Online (Sandbox Code Playgroud) 在C++ 17,为折叠表达式经受短路当用于&&或||作为其操作?如果是这样,这在哪里指定?
在与数学相关的上下文中,我想定义函数的函子<cmath>.出于这个问题的目的,我们将使用std::invoke作为我们的函子.
这是不正确的(现场演示):
std::invoke(std::sin, 0.0);
Run Code Online (Sandbox Code Playgroud)
(g ++ - 8.1)错误:没有用于调用'invoke(<unresolved overloaded function type>,double)'的匹配函数
实际上,std::sin是一个重载集,编译器缺少类型信息来选择其中一个函数.
如何从重载集中命名特定函数?我们可以取代什么LEFT,RIGHT以便以下是良好的形式,并做出预期(例如,选择double std::sin(double))?
#include <functional>
#include <cmath>
int main()
{
(void) std::invoke(LEFT std::sin RIGHT, 0.0);
}
Run Code Online (Sandbox Code Playgroud)
如果这是不可能的,有没有办法定义一个仿函数,所以它是重载集感知?
考虑以下代码:
#include <vector>
#include <algorithm>
#include <ranges>
#include <cassert>
// The type is defined in legacy code and we can not change it
struct A
{
int a;
};
bool operator <(const A &a1, const A &a2)
{
return a1.a < a2.a;
}
int main()
{
assert(A {} < A{}); // OK
std::vector<A> c;
assert(std::ranges::is_sorted(c)); // compilation error
}
Run Code Online (Sandbox Code Playgroud)
可以通过将“spaceship”比较运算符添加到 A 来修复代码:
auto operator<=>(const A &) const = default;
Run Code Online (Sandbox Code Playgroud)
但是,在类外部定义它适用于第一个assert,但不适用于第二个:
auto operator <=>(const A &a1, const A &a2) …Run Code Online (Sandbox Code Playgroud) 考虑以下代码,它使用 C++20 中的 Ranges 库:
#include <vector>
#include <ranges>
#include <iostream>
int main()
{
std::vector<int> v{0,1,2,3,4,5,6,7};
auto transformed = std::ranges::views::transform(v, [](int i){ return i * i; });
std::cout << *std::prev(std::end(transformed));
}
Run Code Online (Sandbox Code Playgroud)
得知(至少在 GCC-10.3.0 和 GCC-12.0.0 下)这段代码卡在std::prev.
什么情况是,由于拉姆达不返回左值引用,transformed范围迭代器被列为输入迭代器(参见规则进行iterator_category选择的views::transform)。但是,std::prev 要求迭代器至少是双向迭代器,所以我猜这段代码实际上是UB。在 libstdc++ 中,应用于std::prev输入迭代器会导致此函数
template<typename _InputIterator, typename _Distance>
__advance(_InputIterator& __i, _Distance __n, input_iterator_tag)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_assert(__n >= 0);
while (__n--)
++__i;
}
Run Code Online (Sandbox Code Playgroud)
被调用__n == -1 …
代码显示了我的问题,我不能使用take(3)之后istream_view。
错误信息是:
/home/linuxbrew/.linuxbrew/Cellar/gcc/11.1.0_1/include/c++/11.1.0/ranges:1775:48: 错误:传递 'std::ranges::take_view<std::ranges::transform_view< std::ranges::basic_istream_view<int, char, std::char_traits >, int ( )(int)> >::_CI' {aka 'const std::counted_iterator<std::ranges::transform_view<std:: range::basic_istream_view<int, char, std::char_traits >, int ( )(int)>::_Iterator >'} 作为 'this' 参数丢弃限定符 [-fpermissive] 1775 | { 返回 __y.count() == 0 || __y.base() == __x._M_end; }
#include <ranges>
using namespace std::views;
using namespace std::ranges;
int to_sq(int a){return a*a;}
int main()
{
auto m_range = istream_view<int>(std::cin);
// error
for (auto i : m_range | transform(to_sq)|take(3))
{
std::cout << i << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用一对作为键来创建无序映射.这对我来说是新的,所以我按照一些教程编写了这个:
struct pair_hash {
template <class T1, class T2>
std::size_t operator () (const std::pair<T1, T2> &p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
return h1 ^ h2;
}
};
int wmain(int argc, wchar_t * argv[])
{
{...}
using Key = std::pair<DWORD, DWORDLONG>;
std::unordered_map<Key, USN, pair_hash> mymap;
std::pair<DWORD, DWORDLONG> mypair(dwVolSN, fileId);
mymap.insert({ mypair, usn });
std::unordered_map<Key, USN>::const_iterator got;
got = mymap.find(mypair); // HERE I GET THE ERROR
return 0
}
Run Code Online (Sandbox Code Playgroud) 只是一些 C++11 代码:
#include<iostream>
#include<atomic>
struct A { int a[4]; };
struct B { int x, y; };
int main()
{
std::cout << std::boolalpha
<< "std::atomic<A> is lock free? "
<< std::atomic<A>{}.is_lock_free() << '\n'
<< "std::atomic<B> is lock free? "
<< std::atomic<B>{}.is_lock_free() << '\n';
}
Run Code Online (Sandbox Code Playgroud)
用mac+clang编译,报错:
Undefined symbols for architecture x86_64:
"___atomic_is_lock_free", referenced from:
_main in atomics.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
clang + …
c++ ×10
c++11 ×3
c++20 ×3
std-ranges ×3
c++17 ×2
atomic ×1
casting ×1
fold ×1
iterator ×1
msdn ×1
overloading ×1
type-punning ×1