小编use*_*253的帖子

静态成员初始值设定项的lambda范围

我的问题是关于静态成员初始值设定项的lambda范围.考虑以下测试:

#include <functional>
#include <iostream>

struct S {
    static const std::function<void(void)> s_func;
};

const std::function<void(void)> S::s_func = []() {
    std::cout << "Hello from " << __PRETTY_FUNCTION__ << std::endl;
};

int main(void) {
    S::s_func();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

从4.8开始的gcc定义了S范围内的lambda,所以程序输出如下:

Hello from S::<lambda()>
Run Code Online (Sandbox Code Playgroud)

(gcc-4.8.2对__FUNCTION__&Co宏有不同的定义,但是lambda仍在其中定义S)

同时gcc-4.7定义了全局范围内的lambda,因此程序输出

Hello from <lambda()>
Run Code Online (Sandbox Code Playgroud)

可能更新的gcc更符合标准.但是,我想询问标准是否实际指定了这个方面,或者它是否可以依赖于实现.

更新:因为@ user5434961建议所有__FUNCTION__相似的宏都依赖于实现,因此最好在符合标准的测试中避免使用它们.所以这里是一个例子,如果编译器在S范围内定义了这样的lambdas 并且打破了编译,则可以编译它:

#include <functional>
#include <iostream>

struct S {
    static const std::function<void(void)> s_func;
private:
    static const int s_field;
};

const std::function<void(void)> S::s_func = []() …
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11

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

使用 java-test-fixtures 模块时,如何从测试中测试声明为“internal”的 kotlin 函数

我试图在我的 kotlin 项目中使用测试装置。不幸的是,我在工具链的一个组件中遇到了一个奇怪的问题,可能是一个错误(不确定是哪个)。

通常,可以从同一包中的单元访问已main声明的 Kotlin 函数。有许多证据支持这一说法,尤其是Kotlin:使单元测试可见的内部函数internaltest

事实上,如果我们有src/main/kotlin/ main.kt

@file:JvmName("Main")
package org.example.broken_test_fixtures

internal fun sayHello(who: String): String = "Hello, $who!"

fun main() {
    println(sayHello("world"))
}
Run Code Online (Sandbox Code Playgroud)

src/test/kotlin/SayHelloTest.kt

package org.example.broken_test_fixtures

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class SayHelloTest {
    @Test
    fun testSayHello() {
        val expected = "Hello, world!"
        val actual = sayHello("world")
        assertEquals(actual, expected)
    }
}
Run Code Online (Sandbox Code Playgroud)

测试通过常规build.gradle.kts成功通过:

plugins {
    kotlin("jvm") version "1.3.61"
}


group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral() …
Run Code Online (Sandbox Code Playgroud)

unit-testing gradle kotlin java-test-fixtures kotlin-gradle-plugin

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

kotlin.time.Duration 和 java 反射

似乎我在 java 反射 API 的交互中遇到了一个奇怪的问题(特别是java.lang.reflect.Proxykotlin.time.Duration。看起来 Java 反射无法确定 的方法返回类型kotlin.time.Duration。考虑以下示例:

@file:JvmName("Main")
package org.test.kotlin.time.duration

import java.lang.reflect.Proxy
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.seconds

@ExperimentalTime
interface I {
    fun getNullableDuration(): Duration? = 1.seconds
    fun getRange(): IntRange = 1..3
    fun getDuration(): Duration = 1.seconds
}

class IC: I

inline fun <reified T> T.sampleProxy(): T = sampleProxy(T::class.java)

fun <T> sampleProxy(c: Class<T>): T {
    return c.cast(Proxy.newProxyInstance(c.classLoader, arrayOf(c)) { _, method, _ ->
        println("[proxy] ${method.declaringClass.name}::${method.name} return type is ${method.returnType}")
        when (method.name) {
            "getNullableDuration", …
Run Code Online (Sandbox Code Playgroud)

java reflection kotlin

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

git commit -m 不接受消息中的空格

我对 Linux 比较陌生,所以这可能是一个简单的问题,但是当我在 git 提交消息周围使用引号(单引号或双引号)时,这些单词将被视为文件名。如何在消息中使用带有空格的 -m - 我不想使用 vi。

\n\n
user@linux:~/Documents/tmp$ git status\n\nOn branch master\n\nInitial commit\n\nChanges to be committed:\n  (use "git rm --cached <file>..." to unstage)\n\n    new file:   afile\n\nuser@linux:~/Documents/tmp$ git commit -m \xc2\xa8this message has spaces and fails\xc2\xa8 \n\nerror: pathspec \'message\' did not match any file(s) known to git.\n\nerror: pathspec \'has\' did not match any file(s) known to git.\n\nerror: pathspec \'spaces\' did not match any file(s) known to git.\n\nerror: pathspec \'and\' did not match any file(s) known to git.\n\nerror: pathspec …
Run Code Online (Sandbox Code Playgroud)

git shell double-quotes

3
推荐指数
1
解决办法
4078
查看次数