这个简单的代码可以用clang ++编译,但不能用g ++编译.它有什么不确定的吗?(模板函数需要使clang高兴)GCC 8.2.0(与-std = c ++ 17一起使用)表示operator <<不明确,它显示了候选列表,但我的模板函数甚至不在其中.
#include <cstddef>
#include <utility>
#include <sstream>
template<class Out>
Out&& operator<<(Out&& out, std::nullptr_t) {
out << "nullptr";
return std::forward<Out>(out); }
struct A : std::stringstream { };
int main() {
A{} << nullptr;
}
Run Code Online (Sandbox Code Playgroud) 考虑以下相对简单的代码:
import scala.deriving.Mirror
trait TC[A]:
def show: A
object TC:
inline def derived[A](using m: Mirror.ProductOf[A]): TC[A] = new TC[A]:
override def show: A =
val p: Product = new Product:
def canEqual(that: Any): Boolean = true
def productArity: Int = 1
def productElement(n: Int): Any = 1
m.fromProduct(p)
sealed trait T
case class C(x: Int) extends T derives TC
case object C // If I remove this line, then no exception will be thrown
@main
def main: Unit =
println(summon[TC[C]].show) …Run Code Online (Sandbox Code Playgroud) 以下引用自C++标准-内存顺序:
如果线程 A 中的原子存储标记为 memory_order_release,并且线程 B 中来自同一变量的原子加载标记为 memory_order_acquire,则从线程的角度来看,在原子存储之前发生的所有内存写入(非原子和宽松原子) A,在线程 B 中成为可见的副作用。也就是说,一旦原子加载完成,线程 B 就保证可以看到线程 A 写入内存的所有内容。
仅在释放和获取同一原子变量的线程之间建立同步。其他线程可以看到与任一或两个同步线程不同的内存访问顺序。
考虑一个原子变量v和以下步骤:
A存储vmemory_order_releaseB存储vmemory_order_releaseC加载vmemory_order_acquire以下陈述是否正确:“线程C保证能看到线程A或的所有内容”B写入内存的所有内容。”
编辑:我将我的评论移到此处以使这一点更清楚。
我在那里引用的 C++ 并没有说B必须阅读A. 它所说的只是在同一个变量上释放/获取A。B这正是我在这三个步骤中所做的:A释放B一些东西,并C获取一些东西。规范中哪里说获取与上一个版本匹配的内容,而不一定是之前版本的任何内容?
考虑以下两行代码:
std::atomic_flag a { }; // Since C++20
std::atomic_flag a = ATOMIC_FLAG_INIT; // Until C++20
Run Code Online (Sandbox Code Playgroud)
在 C++20 中,第一行初始化a为清除状态,但如果我在 C++17 中使用它,a将初始化为未指定的状态。第二行在 C++20 中已弃用,但在 C++ 17 中,它初始化a为清除状态。我怎样才能将它们都包含在代码中?我想我应该使用宏,但是那个宏到底是什么?它适用于所有其他类型的初始化吗std::atomic<T>?
关于如何在 Scala 3 中实现以下功能有什么想法吗?
我同意inline与否,一般来说,轻微的语法改变是可以的(即使是宏也可以)。但我不知道如果没有asInstanceOf.
type Pred[A] = A => Boolean
/** Computes values of predicates and combines them with `&&`.
*
* The implementation does not use `asInstanceOf` or `isInstanceOf` */
inline def tupled[Args <: Tuple](inline preds: Tuple.Map[Args,Pred], inline args: Args): Boolean
Run Code Online (Sandbox Code Playgroud)
例子:
val pred: (Int => Boolean, Int => Boolean) = (_ < 0, _ > 0)
println(tupled[(Int, Int)](pred)( 1, 2)) // should print false
println(tupled[(Int, Int)](pred)(-1, 2)) // should print true
Run Code Online (Sandbox Code Playgroud) What are the possible final results for the variable x in the following C++ code fragment? (please answer based on what is allowed by the C++ standard and not what is currently available on different platforms)
// Inside Thread 0
std::atomic<int> x = {2};
// Inside Thread 1
x.fetch_sub(1,std::memory_order_relaxed)
// Inside Thread 2
x.fetch_sub(1,std::memory_order_relaxed)
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望x最终为零。即使我正在使用,也是这种情况std::memory_order_relaxed吗?
编辑:为了使问题更精确,是否保证 1)在线程 1 和 2 中,返回值是 0 或 1,以及 2)在线程 1 和 2 中的返回值不同。
对 C++ 基本类型(多次读取和多次写入)的变量进行并发非原子读取和写入是 C++ 中的未定义行为吗?我不关心实际值,因为稍后我会发现是否发生了并发读/写,如果发生了,我会忽略当前值。我只想知道行为是否是定义良好的 C++?
如果是良好定义的,它仍然是良好定义的,如果线索1的读/写x和线程2的读/写y在,其中x和y是如下的成员union?
union {
int x;
double y;
};
Run Code Online (Sandbox Code Playgroud)