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
最满意答案
第三个问题来了......在你编辑你的问题之后,仍然是一个开放主题:你的比较器需要一个
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 theGameEntity
class. It should, in order to work with the values of thevector<GameEntity*>
, takeconst 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 ...
-
Scala中的高阶函子(Higher order Functors in scala)[2023-02-13]
不知道你的目标是什么,但是这个类型 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 ... -
应用函子如何配合并行化算法?(How do applicative functors tie in with parallelizing algorithms? (Scala and Scalaz))[2024-01-31]
我把这个问题转给了乔希苏雷斯。 这是他的回答: 迈克 - 我没有太多时间来回应,但我会提供我的意思的例子: 示例#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 ...
-
用适用风格组成的应用函子是否真的是独立的?(Are applicative functors composed with the applicative style really independent?)[2023-03-18]
这里的独立性并不是说一个计算不能检测到其他计算已经运行 - 也就是说,它不应该是parseName对parseEmail没有影响的情况。 相反,您不能使用应用的值 (由parseName解析的名称)来选择接下来要运行的应用计算 :您只能解析通用电子邮件,而不是解析电子邮件地址,同时检查它是否不是包含解析的名称。 另一种说法是,如果仅使用应用函数,则计算的整体“形状”会提前预先确定:您将始终解析一个名称,后跟一个电子邮件地址。 如果您使用了monadic函数,那么您可以根据以前的解析结果决定下一步解析什么,从 ... -
Functors与std :: bind(Functors vs. std::bind)[2024-02-14]
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 ) 它有一个非匿名类型(所以你可以在不使 ... -
声明函子进行比较?(Declaring functors for comparison?)[2024-01-08]
第三个问题来了......在你编辑你的问题之后,仍然是一个开放主题:你的比较器需要一个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 ... -
如何使用STL编写函子?(How to compose functors with STL?)[2022-05-18]
如果你的意思是使用标准库的仿函数组合设施,那么不行,至少在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 ... -
结合函子和单子(Combining functors and monads)[2023-12-09]
一种方法是使用可能的功能: 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 ...