«F(5)»和«int x; F(x)»调用不同的函数? [英] «F(5)» and «int x; F(x)» to call different functions?

查看:164
本文介绍了«F(5)»和«int x; F(x)»调用不同的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写两个不同的函数来处理常量值和给定类型的变量(即 int )。



这是范例测试用例:

  int main ){
int x = 12;
F(5); // this should printconstant
F(x); //这应该打印变量
}



我认为足以定义:

  void F(int v){cout< constant\\\
; }
void F(int& v){cout<< variable\\\
; }

这假设编译器将选择 int& 为变量作为更好的专用和 int 作为唯一的选择)。但是, G ++ 这是结果:

  test.cc:In函数'int main()':
test.cc:13:错误:调用重载的'F(int&)'是不明确的//对于行:F
test.cc:4:注意:候选是:void F(int)
test.cc:5:note:void F(int&)
/ pre>

确实选择 F(int)对于常数,但不知道为变量选择哪个函数。

有没有人知道为什么会发生这种情况?



/ strong>:我在C ++中实验类似prolog的统一方法。能够知道常量和变量之间的差异将有助于在例如 functor(x,5)<=>的情况下选择期望的统一行为(赋值或比较)。 functor(3,5)

解决方案

如果你想要的是区分编译时常数和非编译时间常数 - 那么你没有机会。这是不可能的。



但是如果你想区分一个非常量变量和一个常量变量(和其他所有包括的 - 像文字),那么你可以重载一个函数const引用和非const引用参数。对于这种情况,C ++标准引入了额外的规则,使得这种模糊的情况不会模糊。

  void f(int const&); //#1 
void f(int&); //#2

在这件事上,以下决定完成

  int x = 0; 
int const y = x;
int const z = 1;

f(1); //#1
f(x); //#2
f(y); //#1
f(z); //#1

请注意,它不能区分y和z,是一个编译时常数(称为积分常数表达式,或ICE),而y不是。



您可以做的只是接受编译时的值。重载函数,使一个是模板,另一个不是

  template< int N& void f(); //#1 
void f(int n); //#2

它的行为如下:


$ b b

  int x = 0; 
int const y = x;
int const z = 1;

f< 1>(); //#1
f(1); //#2
f< y>(); // error,y not an ICE
f< z>(); //#1
f(x); //#2


I'd like to write two distinct functions to handle a constant value and a variable of a given type (viz., int).

Here is the example test case:

int main(void) {
        int x=12;
        F(5); // this should print "constant"
        F(x); // this should print "variable"
}

I thought it would be enough to define:

void F(int v) {  cout << "constant\n"; }
void F(int& v) { cout << "variable\n"; }

This assumes that the compiler will choose int& for variables as "better specialized" and int for constants as the only choice). However, G++ this is the result:

test.cc: In function ‘int main()’:
test.cc:13: error: call of overloaded ‘F(int&)’ is ambiguous   // for line: F(x);
test.cc:4: note: candidates are: void F(int)
test.cc:5: note:                 void F(int&)

G++ does choose F(int) for constants but does not know which function to choose for variables.
Does anyone have any idea why this happens?

Background: I am experimenting with prolog-like unification methods in C++. Being able to know the difference between constants and variables would help me choosing desired unification behavior (assignment or comparison) in cases such as functor(x,5) <=> functor(3,5).

解决方案

If what you want is to differentiate between a compile time constant and a non-compile time constant - then you have no chance. That's not possible.

But if you want to differentiate between a non-constant variable and between a constant variable (and everything else included - like literals), then you can overload a function with a const-reference and non-const reference parameter. For this scenario, the C++ Standard introduces extra rules that make this otherwise ambiguous case not ambiguous.

void f(int const&); // #1
void f(int&);       // #2

In this matter, the following decisions are done

int x = 0;
int const y = x;
int const z = 1;

f(1); // #1
f(x); // #2
f(y); // #1
f(z); // #1

Note how it can't differentiate between y and z, even though value of z is a compile time constant (termed integral constant expression, or ICE), while y is not.

What you can do is to only accept compile time values then. Overload the function so that one is a template and the other isn't

template<int N> void f(); // #1
void f(int n);            // #2

It behaves like this then:

int x = 0;
int const y = x;
int const z = 1;

f<1>(); // #1
f(1);   // #2
f<y>(); // error, y not an ICE
f<z>(); // #1
f(x);   // #2

这篇关于«F(5)»和«int x; F(x)»调用不同的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆