小编JeJ*_*eJo的帖子

尽管明确说明了返回类型,但对lambda的调用还是模棱两可的

鉴于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)

c++ lambda implicit-conversion c++11 std-function

11
推荐指数
2
解决办法
290
查看次数

调用参数中的类型名称T是否允许?

抱歉,我无法为这个问题定一个更好的标题。

我已经为此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++ syntax templates language-lawyer c++11

10
推荐指数
1
解决办法
332
查看次数

constexpr成员函数不使用这个?

请考虑以下两个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)

这些程序中的任何一个或两个都不成形吗?

为什么/为什么不呢?

c++ language-lawyer constexpr c++14

9
推荐指数
1
解决办法
2797
查看次数

为什么不能将带有自定义比较lambda的`std :: multiset`用作`std :: map`的值?

这是一个后续问题,询问 如何在不重载operator(),std :: less,std :: greater的情况下为std :: multiset提供自定义比较器?

并且我试图通过以下方式解决。

基本的

可以向类的成员提供自定义比较lambda函数(自以来),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函数(案例-3)

前两个选项是成功的。但是,lambda作为自定义比较功能的情况却无法正常工作。这是MCVC:https ://godbolt.org/z/mSHi1p

#include <iostream>
#include <functional>
#include <string>
#include <map>
#include <set>

const …
Run Code Online (Sandbox Code Playgroud)

c++ lambda custom-compare multiset c++17

9
推荐指数
1
解决办法
395
查看次数

How to provide the function signature for a function taking iterators of stl containers?

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 …

c++ templates stl function c++14

9
推荐指数
3
解决办法
212
查看次数

比较 std::string 和 C 风格的字符串文字

假设我有以下代码:

#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 风格的字符串文字。

据我所知,不能比较不同的数据类型。我在这里缺少什么?

c++ comparison c-strings stdstring c++-standard-library

9
推荐指数
1
解决办法
588
查看次数

如何在模板中返回正确类型的数据?

#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绕过参数doubleint类型时,我想要返回答案是double. 这里的类型,它返回32而不是32.8.

我如何获得我想要的输出?如何编写正确的big函数返回类型?

c++ templates function return-type function-templates

9
推荐指数
3
解决办法
368
查看次数

关于“lambda []void ()-&gt;void”自定义转换的问题

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)

c++ lambda class c++11 std-function

9
推荐指数
1
解决办法
343
查看次数

在 C++20 中实现可变参数 Max 函数

尽管如此,事实上,我们有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)

c++ templates function-templates variadic-templates c++20

9
推荐指数
1
解决办法
278
查看次数

可以在本地使用lambda吗?

是否可以将lambda函数定义为仅在本地使用,即在当前块(函数/方法)中?考虑一些代码将要执行多次的情况(因此将它放入函数中是合乎逻辑的)但它永远不会在块之外使用.

void foo() {
    auto bar = []() {
        // some code applicable only inside foo()
    };

    bar();
    bar();
    bar();
}
Run Code Online (Sandbox Code Playgroud)

bar()声明为正常功能相比,这种方法有哪些优点和缺点?

c++ lambda function code-cleanup

8
推荐指数
4
解决办法
1257
查看次数