以下代码在gcc 7.3.0中编译良好,但不与clang 6.0.0编译.
#include <string>
struct X {
X() : x(10) {}
int operator[](std::string str) { return x + str[0]; }
template <typename T> operator T() { return x; } // (1) fails only in clang
//operator int() { return x; } // (2) fails both in gcc and clang
private:
int x;
};
int main() {
X x;
int y = 20;
int z = int(x);
return x["abc"];
}
Run Code Online (Sandbox Code Playgroud)
我使用命令clang++ 1.cpp -std=c++98指定不同的标准版本.我试过c ++ 98,11,14,17,2a.在所有情况下,错误都是相同的.clang中的错误消息如下:
1.cpp:14:13: error: use …Run Code Online (Sandbox Code Playgroud) 我想实现my_static_assert与c ++ 17单参数稍有不同的方法static_assert:如果在编译时my_static_assert 不知道里面的条件,则应该通过。
my_static_assert以下示例中的第二个应该通过,但是如果我使用static_assert它将会失败。
#include <iostream>
int x, y;
constexpr int f1() { return 0; }
constexpr int f2() { return 0; }
int f3() { return x; }
int f4() { return y; }
constexpr int sum(int a, int b) { return a + b; }
int main() {
std::cin >> x >> y;
// it should fail
my_static_assert(sum(sum(f1(), f2()), sum(f1(), f1())) != 0);
// it should …Run Code Online (Sandbox Code Playgroud) 例如,我想要指定C++标准中CMakeLists.txt:
set_property(TARGET tgt PROPERTY CXX_STANDARD 98)
Run Code Online (Sandbox Code Playgroud)
但是这只有在cmake 3.1.3之后才可用.不幸的是我仍然可以在第一行写CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
Run Code Online (Sandbox Code Playgroud)
我怎样才能确定我指定了正确的要求(3.1.3而不是2.8)?
我想在编译时检查我的字符串文字的长度.现在我正在考虑以下构造,但无法完成它:
#define STR(s) (sizeof(s) < 10 ? s : /* somehow perform static_assert */)
void foo(const char* s) {}
int main() {
foo(STR("abc")); // foo("abc")
foo(STR("abcabcabcabc")); // compile time error: "String exceeds 10 bytes!"
}
Run Code Online (Sandbox Code Playgroud) 在C++中,我经常使用以下模式:
while (int i = getNextElementPlease()) {
printf("%d\n", i);
}
Run Code Online (Sandbox Code Playgroud)
但是在python中我必须重复两次调用:
i = getNextElementPlease()
while (i):
print(i)
i = getNextElementPlease()
Run Code Online (Sandbox Code Playgroud)
如何避免重复getNextElementPlease?
这个版本根本不编译:
struct A {
void foo() {
static_assert(0,"Fail");
}
};
Run Code Online (Sandbox Code Playgroud)
这个版本编译没有错误(至少在我的编译器版本中):
template <int x>
struct B {
void foo() {
static_assert(x,"Fail");
}
};
B<0> b;
Run Code Online (Sandbox Code Playgroud)
第二个版本只有在我调用时才能编译b.foo();,所以我想知道如果我从不调用方法,标准是否允许使用第二个版本foo?所有编译器都会以相同的方式运行吗?是不是未定义的行为?
我希望static_assert在代码中包含一些模板参数满足某些条件时禁止使用模板类的某些方法.这是正确的用法static_assert吗?
.z()在这种情况下,我想使用这种方法(我想禁止在vector只有两个维度时使用):
template <typename T, int D>
struct MyVecctor {
MyVecctor() : data({})
{}
template <typename... Args>
MyVecctor(Args... args) : data({args...})
{
static_assert(D > 0);
static_assert(sizeof...(args) == D);
}
T& operator[] (std::size_t index) {
return data[index];
}
T& x() {
static_assert(D>=1);
return data[0];
}
T& …Run Code Online (Sandbox Code Playgroud) 我试图将合成测试中阵列的对齐从16增加到32,性能从~4100ms降低到~4600ms.如何更高的对齐会损害性能?
下面是我用于测试的代码(我试图在这里使用avx指令).构建g++ test.cpp -O2 -ftree-vectorize -mavx2(我不支持avx512).
#include <chrono>
#include <iostream>
#include <memory>
#include <cassert>
#include <cstring>
#include <cstdlib>
using Time = std::chrono::time_point<std::chrono::system_clock>;
using Clock = std::chrono::system_clock;
template <typename Duration>
auto as_ms(Duration const& duration) {
return std::chrono::duration_cast<std::chrono::milliseconds>(duration);
}
static const int repeats = 10000;
struct I {
static const int size = 524288;
int* pos;
I() : pos(new int[size]) { for (int i = 0; i != size; ++i) { pos[i] = i; } }
~I() { delete pos; …Run Code Online (Sandbox Code Playgroud) 我知道,为了缩短课程名称,我可以做以下事情:
using Time = std::chrono::time_point<std::chrono::system_clock>;
using Clock = std::chrono::system_clock;
Run Code Online (Sandbox Code Playgroud)
但如何正确减少下一行的长度?
/*using Ms = */ std::chrono::duration_cast<std::chrono::milliseconds>
Run Code Online (Sandbox Code Playgroud)
目标代码:
Time start = Clock::now();
// something
Time end = Clock::now();
std::cout << Ms(end - start).count() << std::endl;
Run Code Online (Sandbox Code Playgroud) 我有numpy数组和两个python索引列表,其中的位置可以将数组元素增加一个.numpy有一些方法可以在不使用for循环的情况下对这个操作进行矢量化吗?
我当前执行缓慢:
a = np.zeros([4,5])
xs = [1,1,1,3]
ys = [2,2,3,0]
for x,y in zip(xs,ys): # how to do it in numpy way (efficiently)?
a[x,y] += 1
print(a)
Run Code Online (Sandbox Code Playgroud)
输出:
[[0. 0. 0. 0. 0.]
[0. 0. 2. 1. 0.]
[0. 0. 0. 0. 0.]
[1. 0. 0. 0. 0.]]
Run Code Online (Sandbox Code Playgroud) 我使用CRTP移动的共同实施D1,并D2为模板,然而,构建对象时,我必须做出调用具体到每种类型的重载函数.
以下代码的输出是
D 0x7ffc7d370538
D1 0
D 0x7ffc7d370540
D2 0
Run Code Online (Sandbox Code Playgroud)
虽然预期产量是
D 0x7ffc7d370538
D1 0x7ffc7d370538
D 0x7ffc7d370540
D2 0x7ffc7d370540
Run Code Online (Sandbox Code Playgroud)
为什么dynamic_cast返回nullptr?如何修复此代码?
#include <iostream>
template <typename Derived>
struct B {
B();
virtual ~B() {}
};
struct D1 : B<D1> {
};
struct D2 : B<D2> {
};
void use(D1* d) { std::cout << "D1 " << d << std::endl; }
void use(D2* d) { std::cout << "D2 " << d << std::endl; }
template <typename Derived>
B<Derived>::B() { …Run Code Online (Sandbox Code Playgroud)