这个例子来自Josuttis的C++ templates一书:
#include <iostream>
#include <cstring>
#include <string>
// maximum of two values of any type (call-by-reference)
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
//
所以我今天遇到了关于C++模板编程的问题,谁能给我解释一下A(*)(B)作为模板参数是什么?
template <class X, class Y, class A, class B>
struct replace_type_impl<A(*)(B),X,Y,false>
{
typedef typename replace_type<A,X,Y>::type (*type)(typename replace_type<B,X,Y>::type);
};
有人能解释为什么在这个上下文中参数可以声明为'auto ... arg‘(当在lambda中使用时):
auto glambda = [](auto a, auto&& b) { return a < b; };
bool b = glambda(3, 3.14); // OK
auto vglambda = [](auto printer) {
return [=](auto ... ts) { // OK: ts is a function parameter pack
printer(s
我正在学习c++,我想知道是否有一种方法来定义一个模板,其中返回类型实际上是函数输入的函数。
例如:
calling fun(1) would return me an int
calling fun(2) would return me a float
我想这可以用某种地图来完成吗?
1 <> int
2 <> float
例如,我试图解决的问题是,如果我有一个名为room的对象,我希望有一个名为get_contents的函数,在该函数上,我将传递一个枚举来定义返回类型。例如:
std::vector<Table> tables = room.get_co
我有以下代码:
#include <functional>
std::function<int,int> p;
int main()
{
return 0;
}
我使用的是MinGW g++ 4.8.1,它在
C:\main.cpp|4|error: wrong number of template arguments (2, should be 1)|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\functional|1866|error: provided for 'template<class _Si
我是C++的初学者,正在使用"C++ Primer Plus“这本书自学这门语言,并对编程变得更加自如。
我在书中做了一些与函数模板主题相关的摘录,我有两个问题。
我不明白为什么下面的显式专门化不起作用:
template <typename T>
T lesser(T a, T b)
{
return a > b ? a : b;
}
template <>
box lesser<box>(box& a, box& b)
{
return a.volume > b.volume? a
给定“C++编程语言第4版”中的下列函数模板:
template <typename TT, typename A>
unique_ptr<TT> make_unique(int i, A && a)
{
return unique_ptr<TT>{new TT{ i, std::forward<A>(a) }};
}
我很难理解这到底是怎么回事,
A绝对是一个rvalue,因此make_unique函数似乎在堆上分配其内容,并将该地址保存在unique_ptr中,因此我们不必担心删除它。但是,标准库转发功能是做什么
Stroustrup教授在他的书“C++编程语言第四版”中使用了什么语法?在第917页描述散列和相等函数时。
std::hash<int>{}(variable) //variable is of type int
这是从int到散列的转换吗?但为什么这些花括号后的哈希?我知道,将它们放在变量缺省值之后会初始化它。至于铸造,我们通常像双(int)!