我有一个测试程序来查看编译器(g++)如何匹配模板函数:
#include<stdio.h>
template<class T>void f(T){printf("T\n");}
template<class T>void f(T*){printf("T*\n");}
template<> void f(int*){printf("int*\n");}
int main(int argc,char**) {
int *p = &argc;
f(p); // int*
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它打印int*
. 看来专门的模板是高优先级匹配的?然后我稍微改变了函数声明,这次:
#include<stdio.h>
template<class T>void f(T){printf("T\n");}
template<> void f(int*){printf("int*\n");}
template<class T>void f(T*){printf("T*\n");}
int main(int argc,char**) {
int *p = &argc;
f(p); // T*
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它打印T*
. 这两个程序之间的唯一区别是我稍微更改了重载“f”的函数声明,为什么结果不同?
我通过visual studio创建了一个F#项目,增加了这样的功能:
let fangle a b c =
a+1, b+1, c+1
[<EntryPoint>]
let main argv =
printfn "%i,%i,%i" (fangle 3 4 5) // compile error
Run Code Online (Sandbox Code Playgroud)
Error 2 The type '(int * int * int)' is not compatible with any of the types
byte,int16,int32,int64,sbyte,uint16,uint32,uint64,nativeint,unativeint,
arising from the use of a printf-style format string
C:\Users\mis\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication3\Program.fs 20 25 ConsoleApplication3
Run Code Online (Sandbox Code Playgroud)
为什么这样的错误,在我的函数定义中?
我希望使用batch命令来解析%date%变量,以获取年/月/日,如下所示:
>echo %date
Tue 08/18/2015
Run Code Online (Sandbox Code Playgroud)
我想解析%a = 2015,%b = 08,%c = 18,所以我有.bat如下:
@echo off
rem Get current date and calculate DayOfWeek
for /F "tokens=1-3 delims=/" %%a in ("%date%") do (
set /A mm=%%a, dd=%%b, yy=%%c
)
Run Code Online (Sandbox Code Playgroud)
但它未能执行,说:
Missing operator.
Run Code Online (Sandbox Code Playgroud)
如何修复我的.bat文件?非常感谢.
使用C++模板来知道C风格数组的长度,我们需要这个:
#include<stdio.h>
template<class T,size_t N>
size_t length(T (&a)[N]){ return N; }
int main() {
int fd[2];
printf("%lld\n", length(fd));;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它可以工作,打印2
。我的问题是关于语法的。
在 的声明中length
,为什么参数应该像(&a)[N]
, 一样a[N]
不起作用?
如果我改为
template<class T,size_t N>
size_t length(T a[N]){ return N; }
Run Code Online (Sandbox Code Playgroud)
海湾合作委员会会说:
template argument deduction/substitution failed:
couldn't deduce template parameter 'N'
Run Code Online (Sandbox Code Playgroud)
为什么这里需要&
在数组标识符周围添加一个额外的大括号,这背后的模板规则/语法规则是什么?
感谢详细的解释。
请参阅此线程:https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0806r2.html
它说:
换句话说,一个默认捕获 ([&]) 以拼写出冗余的方式捕获 *this,但另一个捕获 ([=]) 以非冗余方式捕获它。
这表示在 c++17 之前,[=] 捕获this
为值,而 [&] 将捕获 [*this],这是不明确的。所以我进行了一个快速测试,看看 [&] 是否默认捕获 [*this]。
我的测试代码尝试查看 [&] 默认捕获 *this,然后应该调用复制构造函数,并且对其值的任何更改都不会影响原始对象,因为它是副本。
#include<iostream>
using namespace std;
class M{
int mI;
public:
M() : mI(3) { cout << "ctor\n"; }
~M() { cout << "dtor\n"; }
M(const M& m) {
if (this != &m) {
cout << "copy ctor\n";
mI = m.mI;
}
}
M& operator=(const M& m) {
if (this != &m) {
cout << "operator =\n"; …
Run Code Online (Sandbox Code Playgroud) std::atomic<int>
为了比较++ 和std::mutex
protected ++的性能差异int
,我有这个测试程序:
#include <iostream>
#include <atomic>
#include <mutex>
#include <thread>
#include <chrono>
#include <limits>
using namespace std;
#ifndef INT_MAX
const int INT_MAX = numeric_limits<std::int32_t>::max();
const int INT_MIN = numeric_limits<std::int32_t>::min();
#endif
using std::chrono::steady_clock;
const size_t LOOP_COUNT = 12500000;
const size_t THREAD_COUNT = 8;
int intArray[2] = { 0, INT_MAX };
atomic<int> atomicArray[2];
void atomic_tf() {//3.19s
for (size_t i = 0; i < LOOP_COUNT; ++i) {
atomicArray[0]++;
atomicArray[1]--;
}
}
mutex m;
void mutex_tf() {//0.25s
m.lock(); …
Run Code Online (Sandbox Code Playgroud) Write-Host
似乎没有解析变量的属性,这很奇怪。
PS C:\Users\x> $v=@{item1="kkk";item2="aaa"}
PS C:\Users\x> $v.Keys|%{$v.Item($_)}
kkk
aaa
PS C:\Users\x> Write-Host "$v.Count elements"
System.Collections.Hashtable.Count elements
PS C:\Users\x> $v.Count
2
PS C:\Users\x> $v
Name Value
---- -----
item1 kkk
item2 aaa
Run Code Online (Sandbox Code Playgroud)
您会看到,它$v
是一个哈希表,
$v.Count
Run Code Online (Sandbox Code Playgroud)
打印2。但是为什么
Write-Host "$v.Count"
Run Code Online (Sandbox Code Playgroud)
打印出来System.Collections.Hashtable.Count
?这不是我所期望的。
C++ 关键字typeid
有一个魔力:它知道何时使用编译时类型信息以及何时使用运行时类型信息:
#include <iostream>
#include <typeinfo>
using namespace std;
struct Interface { virtual void f() = 0; };
struct S1 : Interface { void f() { cout << "S1\n"; }};
struct S3 : S1 { void f() { cout << "S3\n"; }};
int main() {
S1 s1;
cout << typeid(s1).name() << endl;
S1* ps = new S3;
cout << typeid(ps).name() << endl;
cout << typeid(*ps).name() << endl;
delete ps;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
程序打印:
struct S1
struct S1 * …
Run Code Online (Sandbox Code Playgroud) 我期望子线程的状态,无论成功与否,都不应该使主线程崩溃。
所以我做了一个快速测试:
#include <thread>
#include <iostream>
#include <exception>
using namespace std;
using namespace std::chrono;
void th_function() {
this_thread::sleep_for(chrono::seconds(2));
throw 1; // the criminal here!
}
int main() {
auto th = thread{ th_function };
this_thread::sleep_for(chrono::seconds(1));
cout << "1" << endl;
th.join();
cout << "2" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行结果为:
1
Program stderr
terminate called after throwing an instance of 'int'
Run Code Online (Sandbox Code Playgroud)
好吧,看来throw 1
调用会导致主线程崩溃。我认为对于 C++,每个线程都有自己的运行时堆栈和异常进程链。
那为什么在我的例子中,子线程异常会终止主线程呢?
谢谢。
I've got this code snippet:
int x = 3;
auto fauto = [=](){ cout<<'x'; };
function<void()> func{fauto};
func();
void (*rawPf)() = fauto; // fail to compile
rawPf();
Run Code Online (Sandbox Code Playgroud)
I knew the syntax that only non-capture lambda can be assigned to function pointer. But:
(1) Why std::function can hold capture-lambda?
(2) as both std::function and function pointers are callable
, what's the core difference that makes std::function able to hold capture-lambda, while function pointer cannot?
Any detailed explanation on language design for …
c++ ×7
capture ×2
lambda ×2
templates ×2
arrays ×1
atomic ×1
batch-file ×1
c++11 ×1
cmd ×1
compilation ×1
copy ×1
crash ×1
date ×1
exception ×1
f# ×1
for-loop ×1
function ×1
mutex ×1
overloading ×1
performance ×1
pointers ×1
powershell ×1
properties ×1
return ×1
runtime ×1
this ×1
throw ×1
tuples ×1
typeid ×1
variables ×1
virtual ×1
windows ×1
write-host ×1