enum class E {
One,
Two
};
void foo(E value = decltype(value)::One) {
}
Run Code Online (Sandbox Code Playgroud)
它可以用Clang(3.9)编译,但不能用GCC 6.1编译:value was not declared in this scope.
什么编译器是对的?
各位晚上好.
代码片段将值千言万语:
// Storage suitable for any of the listed instances
alignas(MaxAlign<Base, Derived1, Derived2>::value)
char storage[MaxSize<Base, Derived1, Derived2>::value];
// Instanciate one of the derived classes using placement new
new (storage) Derived2(3.14);
// Later...
// Recover a pointer to the base class
Base &ref = *reinterpret_cast<Base*> (storage);
// Use its (virtual) functions
ref.print();
// Destroy it when we're done.
ref.~Base();
Run Code Online (Sandbox Code Playgroud)
如您所见,我只想通过其基类访问实例,而不实际存储基类指针.请注意,在第二部分中,Derived2类型信息将丢失,因此我只剩下storage并保证一个派生实例在其中.
由于placement new永远不会调整目标指针,因此归结为使用reinterpret_castupcast到基类.现在我知道这很危险,因为static_cast在某些情况下更适合调整指针.[1]
实际上,它会触发未定义的行为.你会在Coliru(g ++ 4.9.0)上找到完整的代码,它会在运行时迅速崩溃.同时在我的电脑上(g ++ 4.8.2),一切都很好.请注意,在g ++ …
我无法将类的类型成员作为模板参数传递.例如,在以下代码中:
std::array<int, 1> a;
std::array<typename a::value_type, 1> a2;
Run Code Online (Sandbox Code Playgroud)
不会编译.
这是什么原因?有解决方法吗?
假设我有一个A大小的方阵n,定义为a std::vector<double>.
std::vector<double> A(n*n);
Run Code Online (Sandbox Code Playgroud)
矩阵的元素以通常的方式访问:
double a_ij = A[i*n + j];
Run Code Online (Sandbox Code Playgroud)
我需要按照相对于第一列的升序对矩阵的行进行排序.
qsort函数允许我使用数组和函数指针来完成它,但我想找到一种方法来实现这一点,使用向量和std::sort.
另外,请注意,出于性能原因,我不希望将矩阵定义为向量矢量.
编辑:
我传递给qsort的函数:
static int comparisonFunction(const void* firstRow, const void* secondRow)
{
if (((double *)firstRow)[0] < ((double *)secondRow)[0]) return -1;
else if (((double *)secondRow)[0] < ((double *)firstRow)[0]) return 1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
电话:
std::qsort(matrixArray, nbRows, sizeof(double)*nbRows, comparisonFunction);
Run Code Online (Sandbox Code Playgroud) 我正在尝试std::vector<std::string>通过Boost.Interprocess 将a转移到新分叉的进程,以便子进程获取它的所有权并将其销毁.检索和读取矢量有效,但是我在破坏它时会遇到访问冲突.我认为分配器开始使用一些指向父母地址空间的指针,并且在孩子的地址空间中没有任何意义.
我应该如何在父母中创建该向量并在孩子中正确销毁?
namespace ip = boost::interprocess;
template <class T>
using ip_allocator = ip::allocator<T, ip::managed_shared_memory::segment_manager>;
template <class T>
using ip_vector = std::vector<T, ip_allocator<T>>;
int CALLBACK WinMain(
_In_ HINSTANCE,
_In_ HINSTANCE,
_In_ LPSTR,
_In_ int
) {
ip::shared_memory_object::remove("MyTestShm");
ip::managed_shared_memory mshm{ ip::open_or_create, "MyTestShm", 1024 * 1024 };
ip_allocator<int> intAlloc{ mshm.get_segment_manager() };
mshm.construct<ip_vector<int>>("ForkData")(
ip_vector<int>{ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, intAlloc}
);
boost::process::spawn(L"Executor.exe", L"ForkData");
}
Run Code Online (Sandbox Code Playgroud)
Executor.exe)代码:int wmain(int argc, wchar_t **argv) try {
namespace …Run Code Online (Sandbox Code Playgroud) 我已经在 Win 和 Linux 上设置了 VSCode for C++,但在代码标记方面遇到了一些问题。在 Win 上,一切都运行良好,但在 Ubuntu 上,VSCode 将标准容器标记为错误。我已经正确包含了所有内容,并且代码编译得很好,我还设置了库/包含搜索路径以进行代码更正。我提供了一个屏幕截图来说明情况。

这里包含路径:

预先感谢您的帮助。
clang version 5.0.0 (trunk 305664)
Target: x86_64-unknown-linux-gnu
Run Code Online (Sandbox Code Playgroud)
以下代码成功编译:
template <int... A>
void f() {
([](auto) {
if constexpr (A == 0)
return 42;
else
return 3.14;
}(0), ...);
}
int main() {
f<0, 1>();
}
Run Code Online (Sandbox Code Playgroud)
......但是这个没有:
template <int... A>
void f() {
([](auto...) { // Variadic lambda
if constexpr (A == 0)
return 42;
else
return 3.14;
}(), ...); // No argument
}
int main() {
f<0, 1>();
}
Run Code Online (Sandbox Code Playgroud)
...屈服:
<source>:7:13: error: 'auto' in return type deduced as 'double' here …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
template <class>
struct Foo_s {
using type = void();
};
template <class>
using Foo_u = void();
template <class T>
struct Bar {
Foo_u<void> foo1; // OK
typename Foo_s<void>::type foo2; // OK
Foo_u<T> foo3; // OK
typename Foo_s<T>::type foo4; // Boom.
};
template struct Bar<void>;
Run Code Online (Sandbox Code Playgroud)
foo4GCC 7.2,Clang 5.0.0和MSVC 19.10.25017 的失败声明.
GCC:
<source>: In instantiation of 'struct Bar<void>':
18 : <source>:18:17: required from here
15 : <source>:15:29: error: field 'Bar<void>::foo4' invalidly declared function type
typename Foo_s<T>::type foo4;
^~~~
Run Code Online (Sandbox Code Playgroud)
铛:
15 …Run Code Online (Sandbox Code Playgroud) 这是我生成错误的示例代码:
#include <functional>
using namespace std;
struct S {
S() = default;
S(const S&) = delete;
S(S&&) = default;
S& operator=(const S&) = delete;
S& operator=(S&&) = delete;
};
template <typename F>
void post(F&& func)
{
function<void()> f{forward<F>(func)};
}
int main()
{
S s;
post([s2 = move(s)] { });
}
Run Code Online (Sandbox Code Playgroud)
拉姆达在里面main(),我捕捉到局部变量s使用std::move().在调用post()之前,s2必须已成功移动构造.
但是,在里面post(),f不能使用对该lambda类型的rvalue引用构造.
如果我删除,s2 = move(s),f可以用这个右值引用来构建.
为什么添加s2 = move(s)渲染lambda不可移动?
在这里,是一个尝试coliru 的链接.
这是重现问题的最小(C++ 14)代码:
template <void (&a)()>
struct Foo {
static auto value() {}
};
void bar() {}
template struct Foo<Foo<bar>::value>;
Run Code Online (Sandbox Code Playgroud)
GNU C++ "g ++(Ubuntu 5.1.0-0ubuntu11~14.04.1)5.1.0"编译器发出:
error: could not convert template argument ‘Foo<a>::value<bar>’ to ‘void (&)()’
template struct Foo<Foo<bar>::value>;
^
Run Code Online (Sandbox Code Playgroud)
我注意到的第一个奇怪的事情是Foo<a>::value<bar>- a没有被替换,并且value以某种方式成为模板?
以下无意义的修复增强了我的印象,这是一个编译器错误:
value()返回void而不是推断它value:template struct Foo<*Foo<bar>::value>;value:template struct Foo<(Foo<bar>::value)>;a指针:template <void (*a)()> struct Foo ...最后,Clang编译我的片段很好.
那么,某个禁止第一个片段的地方是否有一个模糊的标准条款,或者GCC刚刚死在我身上?