有EXT,KHR或AMD或NV扩展.也许还有其他一些.我知道这NV意味着NvidiaAMD不太可能支持这些扩展.但那些khr或ext呢?每个人都强制要求他们支持吗?
我创建了两种类型:bool_t并且number_t我想将一个转换为另一个(以两种方式).但是,我有一些问题要转换bool_t为number_t.
基本上我想做的是(但它不编译):
template<bool v>
struct bool_t {
template<template<int> typename T>
operator T<v ? 1 : 0>() {
return {};
}
};
template<int N>
struct number_t {
template<int n1, int n2>
friend number_t<n1 + n2> operator+(number_t<n1>, number_t<n2>) {
return {};
}
};
int main() {
number_t<0>{} + bool_t<0>{};
}
Run Code Online (Sandbox Code Playgroud)
而错误是:
prog.cc:19:19: error: invalid operands to binary expression ('number_t<0>' and 'bool_t<0>')
number_t<0>{} + bool_t<0>{};
~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
prog.cc:12:26: note: candidate template ignored: could not …Run Code Online (Sandbox Code Playgroud) 基本上,我想要的是诸如multi_types之类的东西 std::initializer_list
template<typename ...As>
struct Foo {
Foo(As...){}
};
template<typename ...As>
void f(Foo<As...>) {}
int main() {
f(Foo{5, 3.f}); // 1) compile
f({}); // 2) compile
f({5, 3}); // 3) error
f({5, 3.8}); // 4) error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我确实理解为什么第一个示例可以编译。但是,我不明白为什么第二个编译,而其他第二个却不编译。对我来说,如果第三个和第四个不编译,则第二个也不应该编译。有没有办法进行第三和第四编译?
假设我们有两个线程。一个给出“去”,一个等待去生产一些东西。
这段代码是正确的还是我可以因为缓存或类似的东西而有一个“无限循环”?
std::atomic_bool canGo{false};
void producer() {
while(canGo.load(memory_order_relaxed) == false);
produce_data();
}
void launcher() {
canGo.store(true, memory_order_relaxed);
}
int main() {
thread a{producer};
thread b{launcher};
}
Run Code Online (Sandbox Code Playgroud)
如果此代码不正确,是否有办法在标准 C++ 中刷新/使缓存无效?
假设我有这样一个元组。
std::tuple<int &, int> tuple{};
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情:
auto [i1, i2] = tuple; // Here i1 is lvalue reference, i2 is int
Run Code Online (Sandbox Code Playgroud)
i1 是左值引用,因为元组上的第一个值是一个左值引用。不过,我没有写auto &[i1, i2]。那么,在这种情况下是否有可能“删除”引用?所以我得到了 i1 和 i2 作为“简单”int。谢谢 !
我想要一个模板化的朋友功能.但是,我不知道如何让它以相同的方式工作,没有模板化的功能.这是一个示例代码
#include <iostream>
namespace ns{
struct Obj {
friend void foo(Obj){std::cout << "no problem" << std::endl;}
template<typename T>
friend void bar(Obj){std::cout << "problem" << std::endl;}
};
}
int main() {
ns::Obj obj;
foo(obj); // Compile
bar<int>(obj); // Not compile
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我知道这段代码是正确的(除了delete未完成):
#include <thread>
#include <atomic>
#include <cassert>
#include <string>
std::atomic<std::string*> ptr;
int data;
void producer()
{
std::string* p = new std::string("Hello");
data = 42;
ptr.store(p, std::memory_order_release);
}
void consumer()
{
std::string* p2;
while (!(p2 = ptr.load(std::memory_order_acquire)))
;
assert(*p2 == "Hello"); // never fires
assert(data == 42); // never fires
}
int main()
{
std::thread t1(producer);
std::thread t2(consumer);
t1.join(); t2.join();
}
Run Code Online (Sandbox Code Playgroud)
但是,我想知道为什么在消费者线程数据中不能成为陈旧数据.是因为acquire手术吗?
我想知道为什么在这段代码中,类型i是一个空的可选项.
auto t = boost::hana::make_tuple(boost::hana::type_c<int>, boost::hana::type_c<double>);
auto i = boost::hana::index_if(t, boost::hana::is_a<boost::hana::type<double>>);
Run Code Online (Sandbox Code Playgroud)
对我来说应该是 optional<hana::size_t<1>>
我知道有Boost hana得到第一个匹配的索引,但它不是完全相同的问题
我确实明白这std::variant适用于不完整的类型。但是,我不明白它是如何工作的,因为根据我的理解,std::variant必须需要它所持有的类型的最大大小。
那么,为什么这段代码不能用s1and编译s2。怎样才能让它发挥作用呢std::variant?
#include <variant>
#include <vector>
#include <type_traits>
#include <typeinfo>
#include <iostream>
struct Rect;
struct Circle;
using Shape = std::variant<Rect, Circle>;
template<typename C>
struct S {static constexpr auto s = sizeof(C);};
constexpr auto s1 = S<Rect>::s;
constexpr auto s2 = sizeof(Rect);
struct Circle{};
struct Rect{
std::vector<Shape> shapes;
};
int main() {}
Run Code Online (Sandbox Code Playgroud) 我认为一切都在问题中。我想知道为什么 unique_ptr 删除是这样处理的:
auto ptr = m_ptr;
m_ptr = newPtr;
delete ptr;
Run Code Online (Sandbox Code Playgroud)
并不是
delete m_ptr;
m_ptr = newPtr;
Run Code Online (Sandbox Code Playgroud)