鉴于lambda的类型是可确定的(可强制转换为std::function(如果我错了,请纠正我)),重载的函数应该同时使用两个函子。已定义?([&]() -> Type {})
请注意,对于我当前的解决方案,我需要按引用捕获,这就是为什么代码包含其逻辑的原因。
以下示例描述了该问题:
#include <iostream>
#include <string>
#include <functional>
void do_some(std::function<void(int)> thing)
{
thing(5);
}
void do_some(std::function<bool(int)> thing)
{
if (thing(10))
{
std::cout << "it's true!" << std::endl;
}
}
int main()
{
int local_to_be_modified = 0;
do_some(
[&](int in)
{
local_to_be_modified = in;
std::cout << "This is void-" << std::endl;
}
);
do_some(
[&](int in) -> bool
{
// error: call to 'do_some' is ambiguous
local_to_be_modified += in;
std::cout << "This is …Run Code Online (Sandbox Code Playgroud) 抱歉,我无法为这个问题定一个更好的标题。
我已经为此SO帖子编写了代码;看起来像:
#include <iostream>
#include <string>
#include <type_traits>
template < typename T, typename ...Ts> T f(const T &a, Ts&&... args)
{
return a;
}
template < typename R, typename T, typename... Ts>
typename std::enable_if<!std::is_same<R, T>::value, R>::type
f(typename T, Ts&&... args)
//^^^^^^^^^^ --->HERE
{
return f<R>(std::forward<Ts>(args)...);
}
int main() {
std::cout << f<int>('a', std::string{ "string" }, 12);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在那里你可以看到,我打错了(大概)
f(typename T, Ts&&... args)
//^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
但是,代码compies与MSVC v19.14时-O3 -std=c++11。
另一方面,GCC和clang提供了编译器错误:https : //godbolt.org/z/HugROb
来自GCC …
请考虑以下两个C++ 14程序:
计划1:
struct S { constexpr int f() const { return 42; } };
S s;
int main() { constexpr int x = s.f(); return x; }
Run Code Online (Sandbox Code Playgroud)
计划2:
struct S { constexpr int f() const { return 42; } };
int g(S s) { constexpr int x = s.f(); return x; }
int main() { S s; return g(s); }
Run Code Online (Sandbox Code Playgroud)
这些程序中的任何一个或两个都不成形吗?
为什么/为什么不呢?
这是一个后续问题,询问 如何在不重载operator(),std :: less,std :: greater的情况下为std :: multiset提供自定义比较器?
并且我试图通过以下方式解决。
可以向类的成员提供自定义比较lambda函数(自c ++ 11以来),std::multiset如下所示:
#include <iostream>
#include <set>
const auto compare = [](int lhs, int rhs) noexcept { return lhs > rhs; };
struct Test
{
std::multiset<int, decltype(compare)> _set{compare};
Test() = default;
};
Run Code Online (Sandbox Code Playgroud)
很简单。
Test班上的成员是
std::map<std::string, std::multiset<int, /* custom compare */>> scripts{};
Run Code Online (Sandbox Code Playgroud)
我尝试使用std::multisetwith自定义
Compare(案例-1)std::greater<> (案例-2)前两个选项是成功的。但是,lambda作为自定义比较功能的情况却无法正常工作。这是MCVC:https ://godbolt.org/z/mSHi1p
#include <iostream>
#include <functional>
#include <string>
#include <map>
#include <set>
const …Run Code Online (Sandbox Code Playgroud) I want to write a function my_func that can be called as so, but does not care that v is a std::vector, it could be any STL container. A bit like std::for_each:
std::vector<std::string> v = {...};
my_func(v.begin(), v.end());
Run Code Online (Sandbox Code Playgroud)
But I cannot figure out the function signature.
void my_func(??? i1, ??? i2)
{
std::for_each(i1, i2, ...); // dumb example implementation
}
Run Code Online (Sandbox Code Playgroud)
I am not great at template programming so even looking at the function declaration for std::for_each is not …
假设我有以下代码:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std; // or std::
int main()
{
string s1{ "Apple" };
cout << boolalpha;
cout << (s1 == "Apple") << endl; //true
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:系统如何在这两者之间进行检查?s1是一个对象,而"Apple"是C 风格的字符串文字。
据我所知,不能比较不同的数据类型。我在这里缺少什么?
#include <iostream>
using namespace std;
template <class X, class Y>
Y big(X a, Y b)
{
if (a > b)
return (a);
else return (b);
}
int main()
{
cout << big(32.8, 9);
}
Run Code Online (Sandbox Code Playgroud)
在这里,我在 CPP 中使用模板,所以当我调用函数big绕过参数double和int类型时,我想要返回答案是double. 这里的类型,它返回32而不是32.8.
我如何获得我想要的输出?如何编写正确的big函数返回类型?
TestCase2并且TestCase3可以正常编译。但是,TestCase1我收到以下错误:
E0312, Custom conversion from "lambda []void ()->void" to
"EventHandler" is not appropriate.
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此错误?我想知道如何解决。
#include <functional>
#include <iostream>
class EventHandler
{
std::function<void()> _func;
public:
int id;
static int counter;
EventHandler() : id{ 0 } {}
EventHandler(const std::function<void()>& func) : _func{ func }
{
id = ++EventHandler::counter;
}
};
int EventHandler::counter = 0;
int main()
{
EventHandler TestCase1 = []() {};
EventHandler TestCase2([]() {});
EventHandler TestCase3 = static_cast<std::function<void()>>([]() {});
}
Run Code Online (Sandbox Code Playgroud) 尽管如此,事实上,我们有std::max,我想尝试是否可以制作一个Max采用可变参数并递归调用Max 以查找最大元素的版本。
我在堆栈溢出中看到了类似的帖子,但这些帖子已经很旧了,而且大多数都std::max在内部使用。由于我有一个特定的错误并使用较新的编译器,因此这篇文章不容易重复。
以下是我写的代码:
#include <iostream>
#include <string>
#include <format>
using namespace std::string_literals;
template <typename T>
constexpr T Max(T&& value)
{
return value;
}
template <typename T, typename... Ts>
constexpr T Max(T&& value, Ts&&... args)
{
const T maxRest = Max(args...);
return (value > maxRest) ? value : maxRest;
}
int main()
{
std::cout << std::format("Maximum integer: {}\n", Max(1));
std::cout << std::format("Maximum integer: {}\n", Max(5, 2, 10, 6, 8));
std::cout << std::format("Maximum integer: …Run Code Online (Sandbox Code Playgroud) 是否可以将lambda函数定义为仅在本地使用,即在当前块(函数/方法)中?考虑一些代码将要执行多次的情况(因此将它放入函数中是合乎逻辑的)但它永远不会在块之外使用.
void foo() {
auto bar = []() {
// some code applicable only inside foo()
};
bar();
bar();
bar();
}
Run Code Online (Sandbox Code Playgroud)
与bar()声明为正常功能相比,这种方法有哪些优点和缺点?
c++ ×10
lambda ×4
templates ×4
c++11 ×3
function ×3
c++14 ×2
std-function ×2
c++17 ×1
c++20 ×1
c-strings ×1
class ×1
code-cleanup ×1
comparison ×1
constexpr ×1
multiset ×1
return-type ×1
stdstring ×1
stl ×1
syntax ×1