小编Lon*_* Bu的帖子

c++ std::bind 通用引用无法编译

#include <functional>
#include <iostream>
#include <type_traits>

using cb_t = std::function<void ()>;

template<typename CB_T, std::enable_if_t<std::is_constructible<cb_t, CB_T>::value, bool> = true>
void
on_my_write(int a, int b, CB_T&& cb) {
    std::cout << "on_my_write:" << a << ", " << b << std::endl;
}
 

template<typename CB_T,  std::enable_if_t<std::is_constructible<cb_t, CB_T>::value, bool> = true>
void foo(CB_T&& cb) {
    auto b = std::bind(&on_my_write<CB_T>, std::placeholders::_1, std::placeholders::_2, std::forward<CB_T>(cb));
    b(1, 2);
}

int main() {
  
    foo([]{});

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

用命令行编译这个

g++ ./try1.cpp -std=c++17
Run Code Online (Sandbox Code Playgroud)

给我:

./try1.cpp: In instantiation of ‘void foo(CB_T&&) [with CB_T …
Run Code Online (Sandbox Code Playgroud)

c++ lambda templates

3
推荐指数
1
解决办法
65
查看次数

JQuery对象与HTMLElementxxx对象

这有效:

$('h1').click(function(){console.log("clicked");};
Run Code Online (Sandbox Code Playgroud)

这不是:

var h1 = $('h1');
h1[0].click(function(){console.log("clicked");}
Run Code Online (Sandbox Code Playgroud)

这又有效:

var h1=$('h1');
var h2 = $(h1[0]).click(function() {console.log("clicked");}
Run Code Online (Sandbox Code Playgroud)

有人会解释原因吗?

非常感谢!

干杯龙

jquery

2
推荐指数
1
解决办法
80
查看次数

标签 统计

c++ ×1

jquery ×1

lambda ×1

templates ×1