我有一个班级X:
class X { ... }
Run Code Online (Sandbox Code Playgroud)
我想做这个:
void f()
{
thread_local static X x = ...;
...
}
Run Code Online (Sandbox Code Playgroud)
(实际上我正在使用gcc所以关键字是"__thread")
但我不能,因为你只能有琐碎的thread_locals.
最好的解决办法是什么?
如果我这样做:
void f()
{
thread_local static X* p = 0;
if (!p)
p = new X(...);
X& x = *p;
...
}
Run Code Online (Sandbox Code Playgroud)
然后:
更新:
这是我到目前为止:
#include <iostream>
#include <type_traits>
using namespace std;
class X { public: X() { cout << "X::X()" << endl; }; ~X() { cout << "X::~X()" << endl; } };
void f()
{
static …Run Code Online (Sandbox Code Playgroud) 以下代码:
#include <iostream>
#include <array>
using namespace std;
constexpr int N = 1000000;
constexpr int f(int x) { return x*2; }
typedef array<int, N> A;
template<int... i> struct F { static constexpr A f() { return A{{ ::f(i)... }}; } };
template<class A, class B> struct C {};
template<int... i, int... j> struct C<F<i...>, F<j...>> : F<i..., (sizeof...(i)+j)...>
{
using T = F<i..., (sizeof...(i)+j)...>;
};
template<int n> struct S : C<typename S<n/2>::T, typename S<n-n/2>::T> {};
template<> struct S<1> : F<0> …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
#include <iostream>
template<class T>
void f(T& t)
{
t = T();
}
int main()
{
int x = 42;
f(x);
std::cout << x;
}
Run Code Online (Sandbox Code Playgroud)
C++ 11标准是否定义了输出是什么?我的编译器输出0,但我的印象是原始类型的默认构造函数是空操作或未定义的行为.
考虑一下hello world C程序:
你好.:
#include "stdio.h"
int main()
{
printf("Hello, World!\n");
}
Run Code Online (Sandbox Code Playgroud)
如果我打电话:
$ gcc -c hello.c -o hello.o
Run Code Online (Sandbox Code Playgroud)
它将生成一个ELF可重定位文件hello.o
如果我然后打电话:
$ gcc hello.o -o hello [1]
Run Code Online (Sandbox Code Playgroud)
它将hello.o与ld链接并生成一个ELF可执行文件hello
但是,如果我直接调用ld [2]而不是[1]:
$ ld hello.o -o hello [2]
Run Code Online (Sandbox Code Playgroud)
我收到这些错误:
/usr/bin/ld.bfd.real: warning: cannot find entry symbol _start
test.c:(.text+0xa): undefined reference to `puts'
Run Code Online (Sandbox Code Playgroud)
gcc必须将其他选项传递给ld(例如链接C库).
反正有没有确定命令行gcc在命令中传递给ld的确切内容[1]?
假设我有一个X类:
struct X
{
...
};
Run Code Online (Sandbox Code Playgroud)
我有一个全局向量V:
vector<X*> V;
Run Code Online (Sandbox Code Playgroud)
我想将V的新实例添加到V中,当且仅当它是动态分配的时(作为完整的大多数派生对象,而不是子对象):
int main()
{
X x; // not added to V
new X; // added to V
struct D : X {};
new D; // not added to V
}
Run Code Online (Sandbox Code Playgroud)
有办法做到这一点吗?也许是以operator new某种方式超载/覆盖?
以下编译没有错误:
#include <memory>
std::unique_ptr<int> f() {
std::unique_ptr<int> x(new int(42));
return x;
}
int main() {
std::unique_ptr<int> y = f();
}
Run Code Online (Sandbox Code Playgroud)
我认为返回值是f()由复制初始化的x,但是std::unique_ptr是一个只移动的类型.怎么这不是格式错误,因为复制构造函数不可用?标准中的相关条款是什么?是否存在某个地方,表示if f()是仅移动类型而不是return语句变为移动构造而不是复制构造?
请考虑以下两个C++ 14程序:
计划1:
struct S { constexpr int f() const { return 42; } };
S s;
int main() { constexpr int x = s.f(); return x; }
Run Code Online (Sandbox Code Playgroud)
计划2:
struct S { constexpr int f() const { return 42; } };
int g(S s) { constexpr int x = s.f(); return x; }
int main() { S s; return g(s); }
Run Code Online (Sandbox Code Playgroud)
这些程序中的任何一个或两个都不成形吗?
为什么/为什么不呢?
我们以下面的6个VkFormats为例:
VK_FORMAT_R8_UNORM
VK_FORMAT_R8_SNORM
VK_FORMAT_R8_USCALED
VK_FORMAT_R8_SSCALED
VK_FORMAT_R8_UINT
VK_FORMAT_R8_SINT
Run Code Online (Sandbox Code Playgroud)
所有这些都指定了具有单个 8 位 R 分量的单分量 8 位格式。
格式的不同在于它们是否 (a)标准化,(b)缩放;或 (c)整数。这意味着什么?这三样东西有什么区别?哪里规定的?
8 位的所有 256 个可能值是否在所有六种格式中都有意义且有效?
(它们的不同之处还在于它们是signed还是unsigned。我认为这意味着它们的底层类型是像 C 类型int8_t还是uint8_t?)
(这源于最近完成的编程竞赛)
您将获得两个10 ^ 5整数的数组,范围为1..10 ^ 7,包括:
int N[100000] = { ... }
int D[100000] = { ... }
Run Code Online (Sandbox Code Playgroud)
想象一下,有理数X是将N的所有元素相乘并除以D的所有元素的结果.
修改两个数组而不更改X的值(并且不指定任何元素超出范围),使得N的乘积和D的乘积没有公因子.
一个天真的解决方案(我认为)会起作用......
for (int i = 0; i < 100000; i++)
for (int j = 0; j < 100000; j++)
{
int k = gcd(N[i], D[j]); // euclids algorithm
N[i] /= k;
D[j] /= k;
}
Run Code Online (Sandbox Code Playgroud)
......但这太慢了.
什么是少于10 ^ 9次操作的解决方案?
考虑以下C++ 11代码:
struct C {};
void f(int(C));
Run Code Online (Sandbox Code Playgroud)
类型是否f相同:
typedef int T(C);
void f(T);
Run Code Online (Sandbox Code Playgroud)
或者是这样的:
void f(int C);
Run Code Online (Sandbox Code Playgroud)
也就是说,应该将(C)其解释为declarator参数名称的一个C,还是作为abstract-declarator函数参数的一个?
标准中指定的是哪里?