在下面的代码中,如何使注释行的工作方式与其正上方的行相同?
我想让它成为一个通用代码,调用模板的合适构造函数Type。
#include <string>
#include <iostream>
template <typename Type>
struct Class
{
Type data;
Class(Type data) : data(data) { }
};
int main()
{
Class<std::string> a = std::string("abc");
// Class<std::string> b = "abc";
std::cout << a.data << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
float test(int len, int* tab)
{
for(int i = 0; i<len; i++)
tab[i] = i;
}
Run Code Online (Sandbox Code Playgroud)
显然回报缺失。对于用于ARM处理器的clang和ndk编译器的这种情况,都会生成一个无限循环。拆卸之后,很明显编译器生成常规分支指令,而不是条件分支。
mov r0, #0
.LBB0_1:
str r0, [r1, r0, lsl #2]
add r0, r0, #1
b .LBB0_1
Run Code Online (Sandbox Code Playgroud)
可以在此处找到带有错误的示例:https : //godbolt.org/z/YDSFw-
请注意,c ++规范指出缺少返回值被视为未定义行为,但仅引用返回值。它不会影响前面的说明。
我在这里想念什么吗?有什么想法吗?
我可以轻松地做到这一点:
auto f = []()->int { return 4; };
auto g = [f]()->int { return f(); });
int i = g();
Run Code Online (Sandbox Code Playgroud)
不过,我不能这样做:
int (*f)() = []()->int { return 4; };
int (*g)() = [f]()->int { return f(); });
int i = g();
Run Code Online (Sandbox Code Playgroud)
为什么我在MSVC中收到这样的消息?
error C2440: 'initializing' : cannot convert from 'ClassName::functionName::< lambda_b2eebcdf2b88a20d8b40b0a03c412089>' to 'int (__cdecl *)(void)'
Run Code Online (Sandbox Code Playgroud)
这发生在线:
int (*g)() = [f]()->int { return f(); });
Run Code Online (Sandbox Code Playgroud)
怎么做得好?
我想传递一个多态lambda函数将执行某些操作(即==或<=或<在不同的数据类型等).所以我想做这样的操作:
bool func(bool (* f)(auto a, auto b))
{
int a = 1, b = 2;
float c = 3, d = 4;
return f(a, b) || f(c, d);
}
Run Code Online (Sandbox Code Playgroud)
以后执行它:
func([](auto a, auto b) -> bool { return a == b; });
func([](auto a, auto b) -> bool { return a <= b; });
func([](auto a, auto b) -> bool { return a < b; });
Run Code Online (Sandbox Code Playgroud)
但遗憾的auto是,不允许以这种方式声明函数指针(带参数类型).我该怎么做呢?
我可以清楚地做这样的事情:
for(int i = 0; i < 10000; i++)
testIteration();
Run Code Online (Sandbox Code Playgroud)
但是是否有任何一行可以做类似事情的std函数?像这样:
std::repeat(10000, testIteration);
Run Code Online (Sandbox Code Playgroud) 正如我们所知, std::vector 在初始化时像std::vector vect(n)orempty_vect.resize(n)不仅分配所需的内存量,而且还用默认值初始化它(即调用默认构造函数)。这会导致不必要的初始化,特别是如果我有一个整数数组并且我想用一些无法通过任何向量构造函数提供的特定值填充它。
另一方面,Capacity 在 call like 中分配内存empty_vect.reserve(n),但在这种情况下 vector 仍然是空的。所以size()返回0,empty()返回true,operator[]产生异常。
现在,请查看代码:
{ // My scope starts here...
std::vector<int> vect;
vect.reserve(n);
int *data = vect.data();
// Here I know the size 'n' and I also have data pointer so I can use it as a regular array.
// ...
} // Here ends my scope, so vector is destroyed, memory is released.
Run Code Online (Sandbox Code Playgroud)
问题是“所以我可以将它用作数组”是否是一个安全的假设?
无论争论,我只是对上述问题感到好奇。无论如何,至于论据: …
我认为在 MSVC++ 中发现了一个错误。或者,这可能是我缺乏知识,我错过了代码中的某些内容。我创建了一个自定义分配器:
#include <forward_list>
#include <iostream>
template <class T>
class Allocator
{
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T *pointer;
typedef const T *const_pointer;
typedef T &reference;
typedef const T &const_reference;
typedef T value_type;
template <class U>
struct rebind
{
typedef Allocator<U> other;
};
Allocator()
{
std::cout << (ptrdiff_t) this << " Allocator()" << std::endl;
}
Allocator(const Allocator &allocator)
{
std::cout << (ptrdiff_t) this << " Allocator(const Allocator &allocator)" << std::endl;
}
template <class U>
Allocator(const Allocator<U> …Run Code Online (Sandbox Code Playgroud) 我想了解如何计算FMA性能。如果我们在这里查看说明:
对于Skylake架构,该指令具有Latency=4和Throughput(CPI)=0.5,因此该指令的整体性能为4*0.5 = 2每条指令的时钟数。
据我了解,如果最大(涡轮)时钟频率为3GHz,那么对于一秒钟的单个内核,我可以执行1 500 000 000条指令。
这样对吗?如果是这样,我观察到性能稍高的原因可能是什么?
我有以下内容:
find . -type f | grep -E '\.(cpp|hpp|c|h)$'
Run Code Online (Sandbox Code Playgroud)
如何仅使用 find 命令来做到这一点?