考虑以下:
struct A { /* ... */ };
A foo() {
auto p = std::make_pair(A{}, 2);
// ... do something
return p.first;
}
auto a = foo();
Run Code Online (Sandbox Code Playgroud)
会p.first被复制,移动还是RVO-ed?
async / await当我遇到以下内容时,我正在玩游戏:
class C
{
private static string str;
private static async Task<int> FooAsync()
{
str += "2";
await Task.Delay(100);
str += "4";
return 5;
}
private static void Main(string[] args)
{
str = "1";
var t = FooAsync();
str += "3";
str += t.Result; // Line X
Console.WriteLine(str);
}
}
Run Code Online (Sandbox Code Playgroud)
我预计结果是"12345",但它是"1235".不知怎的'4'被吃掉了.
如果我将X行分成:
int i = t.Result;
str += i;
Run Code Online (Sandbox Code Playgroud)
然后是预期的"12345"结果.
为什么会这样?(使用VS2012)
有这些<type_traits>:
is_pointer<>
is_function<>
is_member_function_pointer<>
Run Code Online (Sandbox Code Playgroud)
但不是这个:
is_function_pointer<>
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我在使用float类型的默认参数时遇到了一些麻烦:
#include <wchar.h>
#include <iostream>
template<typename T>
void fun(T t = 1e-05);
template<typename T> inline
void fun(T t)
{
std::cout << t << std::endl;
}
int wmain(int argc, wchar_t* argv[])
{
fun<float>();
_getwch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它打印-1.36867e-033而不是1e-05的等价.这里发生了什么?
我正在使用VC++ 10.
EDIT1:
谢谢大家的回复.但是,在以下情况下,转换默认参数不起作用:
template<typename T>
void fun(T t = static_cast<T>(1e-05));
template<typename T> inline
void fun(T t)
{
std::wcout << t << std::endl;
}
int wmain(int argc, wchar_t* argv[])
{
fun<double>();
fun<float>();
_getwch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以这绝对是一个错误,值得报道?
EDIT2:
我在使用decltype成员函数指针时遇到了一些麻烦:
#include <iostream>
#include <type_traits>
struct A
{
void func1() {}
typedef decltype(&A::func1) type;
};
int wmain(int argc, wchar_t* argv[])
{
typedef decltype(&A::func1) type;
//Case 1
std::wcout
<< std::boolalpha
<< std::is_member_function_pointer<type>::value
<< std::endl;
//Case 2
std::wcout
<< std::boolalpha
<< std::is_member_function_pointer<A::type>::value
<< std::endl;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
案例1 true按预期打印,但案例2打印false.
被decltype剥离了一种类型的"成员"属性?如果是这样,为什么?
另外,有没有办法防止这种行为?无论我在哪里使用,我都需要获取成员函数的类型decltype.
请帮忙.
编辑:
使用decltype虚拟成员函数指针是否合法?
以下内容使用VS2012生成内部错误(C1001).
struct C
{
virtual void Foo() {}
typedef decltype(&C::Foo) type; //pointer
}
Run Code Online (Sandbox Code Playgroud)
但这编译很好:
struct C
{
virtual void Foo() {}
typedef decltype(C::Foo) type; //not pointer
}
Run Code Online (Sandbox Code Playgroud)
这是一个错误吗?
我预计以下程序会崩溃,但事实并非如此。它只是打印线程发生异常并正常退出。为什么?
import kotlinx.coroutines.*
fun main() {
val scope = CoroutineScope(Dispatchers.Default)
scope.launch {
delay(100)
throw RuntimeException("1")
}
println("sleep")
Thread.sleep(1000)
println("exit without crash")
}
Run Code Online (Sandbox Code Playgroud)
#include <type_traits>
struct A {};
struct B {
B(A& a) : a(a) {}
//B& operator=(B const& b) = default; // LINE 1
//B& operator=(B const& b) { a = b.a; return *this; } // LINE 2
A& a;
};
static_assert(std::is_copy_constructible<B>::value, ""); // pass
static_assert(std::is_copy_assignable<B>::value, ""); // fail
int main() {
A a;
B b(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器:g ++ 5.1
默认的复制构造函数很好,但默认的复制赋值运算符失败(即使LINE 1未注释).
明确地声明它(取消注释LINE 2)可行.这是一个错误吗?
struct C
{
int Foo(int i) { return i; }
typedef decltype(C::Foo) type;
};
Run Code Online (Sandbox Code Playgroud)
由于没有成员函数类型这样的类型(没有,有吗?),我希望C::type如此int (int).
但以下将无法使用Visual C++ 2012 RC进行编译:
std::function<C::type> f;
Run Code Online (Sandbox Code Playgroud)
那是什么类型的decltype(C::Foo)?