假设我们有一个这样的类模板:
template<typename F>
class A
{
public:
template<typename... Args>
A(F f, Args... args)
{ /* Do something... */ }
};
Run Code Online (Sandbox Code Playgroud)
现在我想以某种方式使用它:
A<int(int)> a(::close, 1);
Run Code Online (Sandbox Code Playgroud)
现在的问题是:有没有办法省略,<int(int)>因为编译器可以知道这些信息::close?无需保存模板的"设计".
至于具体的任务,我需要设计一个类的模板.此类的对象可以在构造时获取此函数的函数和参数,并稍后调用此函数.
比方说,我们需要一个函数模板,它应该根据类型返回一个整数:
template<typename T>
int get_type();
Run Code Online (Sandbox Code Playgroud)
此外,我们确实专门研究了几种类型:
template<>
int get_type<int>()
{
return TYPE_INT;
}
// And so on for other types...
Run Code Online (Sandbox Code Playgroud)
这很有效,但不适用于数组类型。我可以执行以下操作:
template<>
int get_type<char[]>()
{
return TYPE_STRING;
}
Run Code Online (Sandbox Code Playgroud)
编译器“同意”这一点,但链接器不同意。char[]例如,因为类型与char[5].
有没有办法实现这个没有函数参数的函数模板?即,我知道我们可以做这样的事情:
template<typename T>
int get_type(const T&);
Run Code Online (Sandbox Code Playgroud)
但是,实际上这里不需要(使用)函数参数。
编辑:
我使用 C++ 11。
有没有办法在 Linux 命令行中同步 r(?) 同步仅具有相同内容的文件的时间戳?
即,某些目录结构被复制而没有处理时间戳,然后独立修改。现在需要复制相同文件的时间戳只是为了简化进一步的同步。
乡亲们!
有一个Active Directory(Windows)和一个Linux samba客户端。在Active Directory中,策略已以这种方式进行了调整,因此用户需要定期更改其密码(密码具有到期时间)。
我的问题很简单:如果我使用Samba在Linux机器上工作,我可以为给定的用户获得这个到期时间吗?
我有几个代码行:
#include <iostream>
using namespace std;
class A
{
public:
A() noexcept
{
cout << "A::A()" << endl;
}
A(const A&) noexcept
{
cout << "A::A(const A&)" << endl;
}
A(A&&) noexcept
{
cout << "A::A(A&&)" << endl;
}
};
class B
{
public:
B(const A& a) noexcept :
_a(a)
{}
B(A&& a) noexcept :
_a(a)
{}
private:
A _a;
};
int main(int argc, char* argv[])
{
A a;
B b1 = B(a);
B b2 = B(A());
}
Run Code Online (Sandbox Code Playgroud)
他们产生这个输出:
A::A() …Run Code Online (Sandbox Code Playgroud) 比方说,我们要对 进行子类化std::map,并且需要捕获容器中的所有插入和删除操作。例如,为了保存有关容器中存在的密钥的一些特定于应用程序的信息。
如果可能的话,最简单的方法是什么?
也许,最明显的方法是重写执行插入和删除的所有方法和运算符。但我想,这样一来,很容易就会忽略一些东西,不是吗?
我一直认为,必须相反.但当我尝试这个简单的代码时,我得到了意想不到的结果:
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <chrono>
using namespace std;
int main(int argc, char* argv[])
{
int x = 0, y = 0;
double z;
chrono::steady_clock::time_point start_point;
start_point = chrono::steady_clock::now();
for(int i = 0; i < 100000; x = ++i)
for(int j = 0; j < 100000; y = ++j)
z = static_cast<double>(x * y);
cout << "The prefix increment took a " << chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start_point).count() << " milliseconds" << endl;
start_point = chrono::steady_clock::now();
for(int i = …Run Code Online (Sandbox Code Playgroud)