首页 \ 问答 \ 避免显式仿函数模板类型(Avoiding explicit functor template type)

避免显式仿函数模板类型(Avoiding explicit functor template type)

我正在玩弄玩家。 我正在使用下面的标准示例:

class C {
public:
    template <typename Func>
    void foo(Func fun)
    {
        fun();
    }
};

struct S {
    void operator()() { printf ("in S\n"); }
};
....
C myClass;
myClass.foo (S());

这很好用,我不需要在调用foo()时显式提供S模板类型,它只是想出来。 但是假设我想将仿函数存储为成员变量并稍后调用它:

class C {
public:
    template <typename Func>
    void foo(Func fun) {
        _myFunc = fun;
    }

    void someOtherThing() { 
        _myFunc();
    }
private:
    WHAT_IS_THIS_TYPE _myFunc;
};

我现在需要让整个班级成为模板吗? 如果是这样,编译器可以像使用单个仿函数一样推断模板类型,还是我必须明确提供它? 谢谢。


I'm playing with functors. I'm using the standard example below:

class C {
public:
    template <typename Func>
    void foo(Func fun)
    {
        fun();
    }
};

struct S {
    void operator()() { printf ("in S\n"); }
};
....
C myClass;
myClass.foo (S());

This works nicely and I don't need to explicitly provide the S template type in the call to foo(), it just figures it out. But suppose I want to store the functor as a member variable and call it later:

class C {
public:
    template <typename Func>
    void foo(Func fun) {
        _myFunc = fun;
    }

    void someOtherThing() { 
        _myFunc();
    }
private:
    WHAT_IS_THIS_TYPE _myFunc;
};

Do I now need to make the whole class a template? If so, can the compiler infer the template type as it did with the single functor, or must I provide it explicitly? Thanks.


原文:https://stackoverflow.com/questions/18870285
更新时间:2022-05-22 11:05

最满意答案

抖动似乎是由于在悬停时在底部添加额外的2px边框引起的。 这导致文本上升了一点。 要解决此问题,请确保通过将#navBarTop li a更改为以下内容始终为2px边框:

#navBarTop li a {
    display: block;
    padding: 10px 25px;
    text-decoration: none;
    font-family: "Arial";
    color: #00ffff;
    border-bottom: 2px solid transparent;  // <--- add this line
}

这应该可以稳定你的生活。


The jittering seems to be caused by adding the extra 2px border at the bottom when you hover. That causes the text to rise up a bit. To fix this, make sure there's always a 2px border by changing your #navBarTop li a to this:

#navBarTop li a {
    display: block;
    padding: 10px 25px;
    text-decoration: none;
    font-family: "Arial";
    color: #00ffff;
    border-bottom: 2px solid transparent;  // <--- add this line
}

That should stabilize things for you.

相关问答

更多
  • 抖动似乎是由于在悬停时在底部添加额外的2px边框引起的。 这导致文本上升了一点。 要解决此问题,请确保通过将#navBarTop li a更改为以下内容始终为2px边框: #navBarTop li a { display: block; padding: 10px 25px; text-decoration: none; font-family: "Arial"; color: #00ffff; border-bottom: 2px solid transp ...
  • 正如蒂姆塞尔所说,这是你的焦点大纲...虽然不建议你这样做,你可以添加它来摆脱它: ul#main_nav li a { outline-color: transparent; } As Timhessel said, it's your focus outline... although this isn't recommended you could add this to get rid of it: ul#main_nav li a { outline-color: transparent; }
  • 看起来使用display: table比使用当前使用的CSS技巧更容易解决这个问题。 http://jsfiddle.net/rrPKA/ #container { display: table; } .row { display: table-row; } .row > div { display: table-cell; width: 100px; border-bottom: 1px solid lightgray; padding-left: 10px; } . ...
  • 如果添加以下CSS,它应该按预期工作: ul li { position: relative; } ul li:after { position: absolute; bottom: 0; left: 0; } position: relative是确保伪元素受
  • 的宽度和高度而不是整个约束所必需的。 position: absolute; 对于伪元素,您可以将元素锚定到
  • 元素的左下角( bottom: 0; left: 0; )。 If you a ...
  • 使用此代码 - 演示 js - $('li').on('click',function(){ $('li.active').removeClass('active'); $(this).addClass('active'); }); css - li{ float:left; list-style:none; padding: 3px 8px; background: #efefef; cursor: pointer; } li:hover, li ...
  • 使用另一个元素,给外部元素一个固定的高度,并在悬停时使内部项目更小。 我已经添加了背景颜色以显示正在发生的事情: div{ display: inline-block; cursor: pointer; height: 28px; /* lineheight+border+padding */ background: red; } div >span{ border-bottom: 1px solid; display: inline-block; padd ...
  • 简单的语法问题。 在此规则中删除逗号: article a:hover, { ^--- remove this comma 演示: http : //jsfiddle.net/94w8xb3a/2/ Simple syntax problem. Remove comma in this rule: article a:hover, { ^--- remove this comma Demo: http://jsfiddle.net/94w8xb ...
  • 将环与图像本身分开: HTML
    你看到闪烁的原因是因为选择器.vThumb a:hover ~ .vOverlay 。 将.vOverlay悬停在前a元素上时,您只将样式应用于.vOverlay元素。 当.vOverlay元素出现时,它位于a元素上(这意味着您不再悬停在a元素上),导致它消失并重复。 在悬停在.vOverlay时也可以通过应用样式轻松地避免这种情况(而不是仅在将.vOverlay悬停在a元素上时应用它)。 换句话说,您需要以下内容: .vThumb a:hover ~ .vOverlay, .vOverlay:hover ...
  • 我之前使用的一种技术是在元素的顶部添加透明边框和负边距,这样在元素转换时就不可能将元素悬停掉。 .non-active-nav { color: white; -webkit-transition: transform .06s; /* Safari */ transition: transform .06s; border-top: 10px solid transparent; margin-top: -10px; /* equal to border widt ...
  • 相关文章

    更多
  • 《数据结构与STL》(Data Structures and the Standard Template Library)扫描版[PDF]
  • ServletOutputStream cannot be resolved to a type
  • HTTP Content-Type 内容类型详解
  • 《Joomla 2.5 模板教程:宁皓网》(Create a Joomla 2.5 Template)前两章 + 模板资源[光盘镜像]
  • The content of element type "package" must match "...
  • 微信公众号接口添加菜单时错误(errcode":40017 invalid button type)
  • maven项目使用SOLR时报 previously initiated loading for a different type with name "javax/servlet/http/HttpServletRequest" 错的解决方法
  • 模板类怎么在另一个文件中使用?
  • javascript日期相加函数
  • Java变量类型
  • 最新问答

    更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)