首页 \ 问答 \ c ++模板函数用const参数重叠参数推导(c++ template function overlading argument deduction with const argument)

c ++模板函数用const参数重叠参数推导(c++ template function overlading argument deduction with const argument)

我有以下设置:

template <typename T>
void foo(T& t);
void foo(const int& t);

void f()
{
    int i;
    foo(i); //Unresolved reference to "void foo<int>(int &)"
    foo(const_cast<const int&>(i)); //Unresolved reference to "void foo(int const &)"
}

在第一次调用foo时,编译器尝试调用模板版本,因为非模板版本的参数与i的类型不匹配。 在第二次调用中,调用非模板版本。 我使用的是Microsoft C ++编译器版本10.这是标准行为吗? 如果类型不完全匹配,即使它只有一个const修饰符,那么调用模板函数?

编辑:我知道这两个函数没有定义,我只是指出链接器抱怨的内容,以便更清楚编译器想要调用的内容。


I am having the following setup:

template <typename T>
void foo(T& t);
void foo(const int& t);

void f()
{
    int i;
    foo(i); //Unresolved reference to "void foo<int>(int &)"
    foo(const_cast<const int&>(i)); //Unresolved reference to "void foo(int const &)"
}

In the first call to foo, the compiler tries to call the template version, since the argument of the non-template one does not match the type of i. In the second call the non-template version is called. I am using the Microsoft C++ compiler version 10. Is this standard behavior? If the type is not exactly matched, even if it only has a const modifier, then the template function is called?

EDIT: I know those two functions don't have definition, I am just pointing out what the linker complains about, to make it more clear what the compiler wants to call.


原文:https://stackoverflow.com/questions/15601057
更新时间:2022-07-11 21:07

最满意答案

第三个问题来了......在你编辑你的问题之后,仍然是一个开放主题:你的比较器需要一个const &GameEntity类。 它应该为了处理vector<GameEntity*>的值, vector<GameEntity*>使用const GameEntity*参数。


And a third one comes in... After you edited you question, still one open topic: your comparator takes a const & to the GameEntity class. It should, in order to work with the values of the vector<GameEntity*>, take const GameEntity* arguments instead.

相关问答

更多
  • 我们来比较IO应用仿函数和IO monad的Kleisli箭头。 您可以使用箭头来打印由以前的箭头读取的值: runKleisli ((Kleisli $ \() -> getLine) >>> Kleisli putStrLn) () 但是你不能用应用函数来做到这一点。 通过应用函数,所有的效果都会在将函数中的函数应用到函数中的参数之前发生。 函数中的函数不能使用函数参数中的值来“调整”它自己的效果,可以这么说。 Let's compare the IO applicative functor with ...
  • 为什么不接受功能呢? 那是因为std::set第二个模板参数应该是一个类型。 具体来说,它应该是比较的仿函数类型。 您正在传递一个函数(指针),它不是一个类型。 Why doesn't it accept a function? That's because std::set second template argument is supposed to be a type. Specifically it's supposed to be the functor type for the comparis ...
  • 不知道你的目标是什么,但是这个类型 import scala.language.higherKinds trait Functor1[F[G[_]]]{ def hmap[X[_], Y[_]]:(X ~> Y) => F[X] => F[Y] } case class FId[Z,F[_]](f:F[Z]) implicit def Functor1Id[Z] = new Functor1[({type L[G[_]]=FId[Z,G]})#L]{ def h ...
  • 我把这个问题转给了乔希苏雷斯。 这是他的回答: 迈克 - 我没有太多时间来回应,但我会提供我的意思的例子: 示例#1 - 表单验证。 我想针对输入运行一些验证并汇总所有错误,即并行检测它们。 通过应用功能,我可以做到这一点。 因此,给定一组“处理”功能,如下所示: def processUser(data: Data): Validation[User] = { if (data get "username" isEmpty) Failure("username must not be empty") ...
  • 函数函数几乎只是一个定义operator()的类。 这样可以创建一个“看起来像”的对象: // this is a functor struct add_x { add_x(int x) : x(x) {} int operator()(int y) const { return x + y; } private: int x; }; // Now you can use it like this: add_x add42(42); // create an instance of the ...
  • 这里的独立性并不是说一个计算不能检测到其他计算已经运行 - 也就是说,它不应该是parseName对parseEmail没有影响的情况。 相反,您不能使用应用的值 (由parseName解析的名称)来选择接下来要运行的应用计算 :您只能解析通用电子邮件,而不是解析电子邮件地址,同时检查它是否不是包含解析的名称。 另一种说法是,如果仅使用应用函数,则计算的整体“形状”会提前预先确定:您将始终解析一个名称,后跟一个电子邮件地址。 如果您使用了monadic函数,那么您可以根据以前的解析结果决定下一步解析什么,从 ...
  • lambda解决方案将是idomatic C ++ 11的方式: auto a = [&]( int x ){ return superComplexAlgorithm( 3, 4, 5, x ); }; for( unsigned int i = 0; i < 100; ++i ) do_stuff_with( a ); 使用手写函数的优点是您可以: 将参数移入捕获或转换它们(您可以使用C ++ 1y中的lambda来实现,但还不行 - 这也可以通过bind ) 它有一个非匿名类型(所以你可以在不使 ...
  • 第三个问题来了......在你编辑你的问题之后,仍然是一个开放主题:你的比较器需要一个const &到GameEntity类。 它应该为了处理vector的值, vector使用const GameEntity*参数。 And a third one comes in... After you edited you question, still one open topic: your comparator takes a const & to the ...
  • 如果你的意思是使用标准库的仿函数组合设施,那么不行,至少在C ++ 98中不行。 在C ++ 11中,你可以使用std::bind来获得任意组合的函数: using std::placeholders; int count = std::count_if(v.begin(), v.end(), std::bind(std::logical_and(), std::bin ...
  • 一种方法是使用可能的功能: y :: A -> Bool y a = maybe False h (g $ f a) 或者Zeta指出,您可以使用无点符号: y = maybe False h . (g . f) One way would be to use the maybe function: y :: A -> Bool y a = maybe False h (g $ f a) Or as Zeta points out, you can use the pointfree notati ...

相关文章

更多

最新问答

更多
  • 您如何使用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)