小编luk*_*keg的帖子

什么是C++ 20的字符串文字运算符模板?

什么是C++ 20的字符串文字运算符模板?Cppreference 在这方面的例子非常简洁,对我来说不是很清楚:

struct A { A(const char *); auto operator<=>(const A&) const = default; };

template<A a> A operator ""_a(); 
Run Code Online (Sandbox Code Playgroud)

在试图理解这个特性是什么时,我刚学会了你可以在C++中使用数字文字运算符模板,这使得数字常量的每个数字都作为非类型参数传递给模板(参见这里更好的解释) .目前,文字运算符模板不支持字符文字,尽管有编译器扩展可以实现这一点.我不认为C++ 20的 字符串文字运算符模板与此有关,因为我已经了解到扩展文字运算符模板以处理字符文字的提议在委员会中被否决了?

c++ user-defined-literals c++20

21
推荐指数
1
解决办法
2090
查看次数

Spring AOP没有@EnableAspectJAutoProxy可以工作吗?

我正在学习Spring(当前是其AOP框架)。即使我阅读过的所有消息都说要启用AOP,也需要使用@EnableAspectJAutoProxy注释(或其XML副本),但我的代码似乎可以注释掉注释。那是因为我使用Lombok还是Spring Boot(v。1.5.9.RELEASE,取决于Spring v。4.3.13.RELEASE)?

最小的示例如下:

build.gradle

buildscript {
    ext {
        springBootVersion = '1.5.9.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

group = 'lukeg'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    compileOnly('org.projectlombok:lombok')
    compile("org.aspectj:aspectjweaver:1.8.11")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}
Run Code Online (Sandbox Code Playgroud)

ApplicationConfiguration.java(请注意,AOP注释已被注释掉)

package lukeg;

import org.springframework.context.annotation.*;

@Configuration
@ComponentScan
//@EnableAspectJAutoProxy
public class ApplicationConfiguration {
    @Bean
    TestComponent testComponent() {
        return new TestComponent();
    }
}
Run Code Online (Sandbox Code Playgroud)

LearnApplication.java

package lukeg;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import …
Run Code Online (Sandbox Code Playgroud)

java spring aspectj spring-aop

9
推荐指数
1
解决办法
8231
查看次数

使用泰语数字以Java显示日期时间

这个练习来自Horstmann的书籍Core Java,因为他不耐烦:

编写一个程序,演示[...]泰国的日期和时间格式样式(泰语数字).

我尝试使用以下代码解决练习:

    Locale locale =  Locale.forLanguageTag("th-TH-TH");
    LocalDateTime dateTime = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
    System.out.println(formatter.withLocale(locale).format(dateTime));
Run Code Online (Sandbox Code Playgroud)

问题是虽然月份的名称是用泰语给出的(至少我是这么认为的,因为我不懂泰语),数字仍然用阿拉伯数字格式化,输出如下:

3ก.ย.2017,22:42:16

我尝试了不同的语言标记("th-TH","th-TH-TH","th-TH-u-nu-thai"),都无济于事.我应该更改什么才能使程序按预期运行?我在Windows 10 64位上使用JDK 1.8.0_131.

java datetime localization datetime-format

6
推荐指数
1
解决办法
626
查看次数

C++指向函数声明语法的指针

foo参数的情况下,两个声明之间有什么区别?第二个语法对我来说很熟悉,并声明了一个指向函数的指针.这两个声明完全相同吗?

void foo(int(int));
void foo(int(*)(int));
Run Code Online (Sandbox Code Playgroud)

c++ function-declaration

6
推荐指数
1
解决办法
134
查看次数

std :: condition_variable中可能的竞争条件?

我查看了VC++的实现std::condition_variable(lock,pred),基本上,它看起来像这样:

template<class _Predicate>
        void wait(unique_lock<mutex>& _Lck, _Predicate _Pred)
        {   // wait for signal and test predicate
        while (!_Pred())
            wait(_Lck);
        }
Run Code Online (Sandbox Code Playgroud)

基本上,裸wait调用_Cnd_waitX调用_Cnd_wait哪些调用do_wait哪些调用cond->_get_cv()->wait(cs);(所有这些都在文件cond.c中).

cond->_get_cv()回报Concurrency::details::stl_condition_variable_interface.

如果我们转到该文件primitives.h,我们会看到在Windows 7及更高版本下,我们有一个stl_condition_variable_win7包含旧的win32 CONDITION_VARIABLEwait调用的类__crtSleepConditionVariableSRW.

进行一些汇编调试,__crtSleepConditionVariableSRW只需提取SleepConditionVariableSRW函数指针,然后调用它.

事情就是这样:据我所知,win32 CONDITION_VARIABLE不是内核对象,而是用户模式对象.因此,如果某个线程通知此变量并且没有线程实际上在其上休眠,则您丢失了通知,并且线程将保持休眠状态,直到超时或其他某个线程通知它.一个小程序实际上可以证明它 - 如果你错过了通知点 - 你的线程将保持睡眠,虽然其他一些线程通知它.

我的问题是这样的:
一个线程等待条件变量,谓词返回false.然后,发生上面解释的整个呼叫链.在那个时候,另一个线程改变了环境,因此谓词将返回true 通知条件变量.我们在原始线程中传递了谓词,但我们仍然没有进入SleepConditionVariableSRW- 调用链很长.

所以,虽然我们通知了条件变量并且条件变量上的谓词肯定会返回true(因为通知程序是这样做的),我们仍然会阻塞条件变量,可能永远.

这是怎么表现的?这似乎是一个巨大的丑陋竞争条件等待发生.如果您通知条件变量并且它的谓词返回true - 则该线程应该解除阻塞.但是,如果我们在检查谓词和睡觉之间处于不确定状态 - 我们将永远被阻止.std::condition_variable::wait不是原子功能.

标准对此有何看法,是否真的是竞争条件?

c++ multithreading synchronization condition-variable

5
推荐指数
1
解决办法
1300
查看次数

Python - 额外的关键字(?)和继承

typing.py(从与 Anaconda 捆绑在一起的 Python 3.6.6 开始)声明List类如下:

class List(list, MutableSequence[T], extra=list):

据我的理解,这意味着List类继承自listMutableSequence[T])。extra继承列表中的赋值是什么意思?

python python-3.x

5
推荐指数
1
解决办法
1911
查看次数

std::min/float monoid 的标识元素

这个(非常有趣的)演讲中,演讲者提出了一个问题:

float/std::min 幺半群的 e 值是多少。

换句话说:由标准 C++ 浮点数和 std::min 操作组成的幺半群的单位元素是什么?演讲者说答案是“有趣的”。

我认为这std::numeric_limits<float>::infinity()应该是答案,正如代码所证明的那样:

  const auto max = numeric_limits<float>::max();
  const auto min = numeric_limits<float>::min();
  const auto nan = numeric_limits<float>::signaling_NaN();
  const auto nan2 = numeric_limits<float>::quiet_NaN();
  const auto inf = numeric_limits<float>::infinity();
  const auto minus_inf = -inf;
  cout << std::min(max, inf) << "\n";
  cout << std::min(min, inf) << "\n";
  cout << std::min(nan, inf) << "\n";
  cout << std::min(nan2, inf) << "\n";
  cout << std::min(inf, inf) << "\n";
  cout << std::min(minus_inf, inf) << …
Run Code Online (Sandbox Code Playgroud)

c++ monoids

5
推荐指数
1
解决办法
147
查看次数

标准C++事务内存状态

C++ 17的事务性内存提议的当前状态是什么.是否会包含在标准中,旨在包含在标准C++的某些未来版本中,或者仅仅是一个实验性的概念验证功能,其标准化状态仍未确定?

我问,因为一些标准化委员会的文件似乎在这里提供了相互矛盾的沟通.一方面,我们有P0265R0(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0265r0.pdf)说交易记忆不会标准化,另一方面手 - 有一篇由Stroustrup撰写的N4492论文(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4492.pdf),其中的事务内存列在C++ 17功能列表中.

c++ transactional-memory c++17

4
推荐指数
1
解决办法
482
查看次数

`ByteBuffer.allocateDirect`和Xmx

根据各种 来源(虽然在JavaDoc中没有特别提到),ByteBuffer.allocateDirect从主JVM堆中分配内存.我可以确认使用Java Mission Control,看到调用的程序ByteBuffer n = ByteBuffer.allocateDirect(Integer.MAX_VALUE)没有使用大量的Java Heap内存:

在此输入图像描述

但是,当限制JVM堆内存时,此堆外内存分配将停止工作.例如,当我使用-Xmx1g选项运行JVM时,allocateDirect调用会导致以下异常:Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory.我不完全理解这个JVM选项如何与堆外直接内存分配相关,因为 - 根据文档 - 该-Xmx选项设置Java堆空间大小.如果我使用getUnsafe().allocateMemory(Integer.MAX_VALUE);内存分配内存成功分配.我的JVM如下:

java版"10"2018-03-20 Java(TM)SE运行时环境18.3(版本10 + 46)Java HotSpot(TM)64位服务器VM 18.3(版本10 + 46,混合模式)

就是这种行为之间的XmxByteBuffer.allocateDirect预期?

编辑:JDK 1.7中似乎存在(不可重现的)错误,其行为与上述相同.这是一个错误吗?

java memory-management

4
推荐指数
1
解决办法
570
查看次数

将 preincrement 结果强制转换为 void 的原因

cppreference为可能的for_each_n实现提供了以下代码:

template<class InputIt, class Size, class UnaryFunction>
InputIt for_each_n(InputIt first, Size n, UnaryFunction f)
{
    for (Size i = 0; i < n; ++first, (void) ++i) {
        f(*first);
    }
    return first;
}
Run Code Online (Sandbox Code Playgroud)

为什么是++icast to的结果void?是因为我们丢弃了++i抑制可能的编译器警告的结果吗?如果是这样,为什么++firstnot cast tovoid也是如此?因为operator ,隐式丢弃它?

c++

4
推荐指数
1
解决办法
78
查看次数

为什么CPU推测执行不会导致OOB程序崩溃?

这些问题源于阅读Spectre 攻击论文。如果我理解正确的话,攻击源于 CPU 启发式推测执行(错误)代码分支的可能性。考虑这个例子(C 语言):

int arr[42];
if (i < 42) {
    int j = arr[i];
}
Run Code Online (Sandbox Code Playgroud)

如果我正确理解了这篇论文,int j = arr[i]即使在i >= 42. 我的问题是 - 当我访问超出其范围的数组时,我的程序经常会崩溃(Linux 上的分段错误,Windows 上的“程序执行了非法操作”错误)。

为什么在数组越界访问的情况下推测执行不会导致程序崩溃?

memory security cpu cpu-architecture speculative-execution

2
推荐指数
1
解决办法
500
查看次数

Kotlin 方法引用代替 lambda

考虑以下代码,我们获取列表的块,将它们连接起来并打印到标准输出:

val l = listOf("1", "2", "3", "4", "5", "6", "7")
l.chunked(3, { a -> a.joinToString()}).forEach(::println)
Run Code Online (Sandbox Code Playgroud)

该代码可以正常工作。我想改变 lambda 调用({ a -> a.joinToString()} ) 更改为方法引用,如下所示:

l.chunked(3, l::joinToString).forEach(::println)
Run Code Online (Sandbox Code Playgroud)

使用方法引用的代码无法编译,给出的错误是:

错误:(4, 7) Kotlin: 类型推断失败: fun Iterable.chunked(size: Int, transform: (List) -> R): List cannot be applied to receiver: List arguments: (Int,KFunction6<@ParameterName CharSequence , @ParameterName CharSequence, @ParameterName CharSequence, @ParameterName Int, @ParameterName CharSequence, @ParameterName(name = "transform") ((String) -> CharSequence)?, String>)

错误:(4, 18) Kotlin:类型不匹配:推断类型为 KFunction6<@ParameterName CharSequence, @ParameterName CharSequence, @ParameterName CharSequence, @ParameterName Int, @ParameterName CharSequence, @ParameterName(name = …

methods kotlin

2
推荐指数
1
解决办法
1057
查看次数