我正在努力清理一个充满功能模板的API,并且强烈希望编写以下代码.
template <typename T, typename U, typename V>
void doWork(const T& arg1, const U& arg2, V* optionalArg = 0);
Run Code Online (Sandbox Code Playgroud)
当我调用此模板时,我想按照以下方式执行此操作.
std::string text("hello");
doWork(100, 20.0, &text);
doWork('a', text); // oops!
doWork<char, std::string, void>('a', text); // to verbose!
Run Code Online (Sandbox Code Playgroud)
不幸的是,第二次调用没有编译,因为编译器无法推断出可选参数的类型.这很不幸,因为我真的不在乎参数类型是什么,而是它的值是NULL.此外,我想避免第三次调用的路由,因为它妨碍了可读性.
这导致我尝试使模板参数V具有默认类型,这也不起作用,因为您不能将默认类型应用于函数模板参数(至少使用VC++ 9.0).
template <typename T, typename U, typename V = void> // oops!
void doWork(const T& arg1, const U& arg2, V* optionalArg = 0);
Run Code Online (Sandbox Code Playgroud)
我唯一剩下的选择是引入一个doWork对模板参数一无所知的重载V.
template <typename T, typename U>
void doWork(const T& arg1, const U& arg2) …Run Code Online (Sandbox Code Playgroud) c++ templates optional-parameters optional-arguments function-templates
我已经尝试了几乎所有可以想象的东西(当然除了正确的东西),但仍然无法理解为什么我会得到一个模棱两可的错误.我相当肯定这是一件非常愚蠢的事情但我却看不到它!我的编译器显示插入操作符的警告,我知道他们都被调用了但是我被告知坚持旧的virtual会帮助我(并且它没有...),但是还没有!
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
template <class T>
T produceReport(string title, T accType, int tableRows)
{
cout << title << endl;
for (int x = 0; x < tableRows; x++)
{
cout << "-";
}
cout << endl << accType;
};
class BankAccount
{
private:
int accNum;
double accBal;
public:
BankAccount(int = 0, double = 0.0);
void enterAccountData();
void displayAccount();
};
BankAccount::BankAccount(int num, double bal)
{
accNum = num;
accBal = bal;
}
void …Run Code Online (Sandbox Code Playgroud) 以下位代码在GCC 4.5.3中编译,但不在VS 2008和2010中编译.这是由于VS编译器错误还是标准禁止给出默认函数模板参数值?
#include <cstdlib>
struct Bar
{
enum Group{ A , B , C };
};
struct Foo
{
template<typename T>
static void getSome( typename T::Group = T::A );
};
template<typename T>
void Foo::getSome( typename T::Group )
{
};
int main()
{
Foo::getSome<Bar>(); // Does not compile in VS 2008 & 2010 (compiles in gcc 4.5.3)
Foo::getSome<Bar>( Bar::C ); // Compiles in VS 2008 and gcc 4.5.3
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
错误信息
prog.cpp(11) : error C2589: '::' : illegal token …Run Code Online (Sandbox Code Playgroud) 我有几个模板函数的实例。它们中的每一个都按顺序执行每个给定的 lambda,并附带特定的消息。当我使用一个 lambda 执行此操作时,一切正常,但是当我尝试添加多个 lambda 时,我得到
note: candidate template ignored: deduced conflicting types for parameter 'Task'
Run Code Online (Sandbox Code Playgroud)
来自铿锵。这是我的代码:
template <class Task> void doTasks(Task task1) // works fine
{
if (std::__is_invocable<Task>::value)
{
std::cout << "doing first task" << endl;
task1;
}
}
template <class Task>
void doTasks(Task task1, Task task2) // deduced conflicting types
{
if (std::__is_invocable<Task>::value)
{
std::cout << "doing first task" << endl;
task1();
std::cout << "doing second task" << endl;
task2();
}
}
int main()
{
doTasks([&] …Run Code Online (Sandbox Code Playgroud) c++ templates function-templates template-argument-deduction c++17
我有以下类,该类有一个名为的方法errorHandler,需要使用多个不同的回调:
class IOPin;
class IOPinHandler
{
IOPinHandler();
virtual ~IOPinHandler();
static bool ptrFun(IOPinHandler&) { return true; };
template<typename T>
bool init(IOPin& obj, const T& param);
template<typename HandlerReturn = void, typename ...Args>
HandlerReturn errorHandler(const GpioStatusCode& code, HandlerReturn(*callback)(IOPinHandler& obj, const Args&...), const Args&... args);
// Added this overload to support passing lambdas as arguments,
// however does not seems to be called
template<typename HandlerReturn = void, typename ...Args>
HandlerReturn errorHandler(const GpioStatusCode& code, const std::function<HandlerReturn(IOPinHandler&, const Args&...)>& callback, Args&... args);
};
Run Code Online (Sandbox Code Playgroud)
我可以使用以下内容: …
我有一段 C++ 代码:
#include <iostream>
#include <string>
#include <map>
static counter = 0;
class Probe
{
private:
int supply_;
Probe(const Probe&);
public:
Probe()
{
supply_ = 10000;
}
int get_supply()
{
return supply_;
}
};
/********************************************************************************
template<class T> T Create(int counter, T& produced)
{
produced[counter] = new ; // ??????????????????????????????????????
return produced;
}
************************************************************************************/
std::map<int, Probe*> CreatInitWorkers(int counter, std::map<int, Probe*> &init_workers)
{
init_workers[counter] = new Probe();
return init_workers;
}
int main()
{
std::map<int, Probe*> workers;
for (int i = 0; …Run Code Online (Sandbox Code Playgroud) 由于8.3.6 ([dcl.fct.default])/4,
在给定的函数声明中,带有默认参数的参数后面的每个参数都应具有在此或先前声明中提供的默认参数,或者应是函数参数包。
应编译以下内容:
#include <iostream>
template<typename ...Ts>
void foo(int i=8, Ts... args)
{
std::cout << "Default parameters \t= " << i << "\n";
std::cout << "Additional params \t= " << sizeof...(Ts) << "\n";
}
int main()
{
foo(); // calls foo<>(int)
foo(1, "str1", "str2"); // calls foo<const char*, const char*>(int, const char*, const char*)
foo("str3"); // ERROR: does not call foo<const char*>(int, const char*)
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但它无法编译,因为foo("str3")这让编译器感到困惑。它抱怨没有匹配的函数可供调用foo(const char*),并且无法将"str3"(type const char* …
c++ default-value function-templates variadic-templates explicit-instantiation
为了轻松地重写一个大的 switch-case,我正在尝试像这样的 constexpr 函数模板(它只是一个只有 cout 的简化版本):
#include <iostream>
template<int T>
constexpr void bigSwitchCase(const int idx)
{
if (T == 1 && T == idx)
{
std::cout << 1 <<std::endl;
return;
}
if (T == idx)
{
std::cout << T << std::endl;
}
else if (T > 1)
{
bigSwitchCase<T - 1>(idx);
}
return;
}
template<>
void bigSwitchCase<1>(const int idx)
{
if (1== idx)
{
std::cout << 1 <<std::endl;
return;
}
}
int main()
{
bigSwitchCase<64>(15);
//bigSwitchCase<4096>(15);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但问题是它生成的代码不如以下版本那么快: …
我想计算提供给 function 的任意数量的参数的总和sum。假设给予函数的整数将满足operator+.
如果我注释掉该函数sum()(没有参数的函数),则代码无法编译。如果我取消注释,代码会编译并运行,但永远不会命中 function sum()。
我似乎无法理解为什么我们需要有sum()功能,因为我正在使用条件sizeof...(Args)
如果有人能帮助我理解这一点,我会非常感激吗?
/*
int sum()
{
std::cout << "Sum with 0 Args" << std::endl;
return 0;
}
*/
template <typename T, typename...Args>
T sum(T first, Args...args)
{
// std::cout << sizeof...(Args) << std::endl;
if (sizeof...(Args) != 0)
{
return first + sum(args...);
}
else
{
std::cout << "Found 0 args" << std::endl;
return first;
}
}
int main()
{
std::cout << sum(1, 2, 3) …Run Code Online (Sandbox Code Playgroud)