在以下场景中,operator<<函数参与重载解析(当它位于全局命名空间中时):
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
os << '[';
for (const T &val : vec)
os << val << ", ";
return os << "\b\b]";
}
Run Code Online (Sandbox Code Playgroud)
...但是下面,当在名称空间内定义时,它不会:
namespace blah {
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
os << '[';
for (const T &val : vec)
os << val << ", ";
return os << "\b\b]";
}
}
Run Code Online (Sandbox Code Playgroud)
当从 调用时main,如下所示:
int main() {
std::vector<int> vec{5, 4, 3, 6, …Run Code Online (Sandbox Code Playgroud) c++ namespaces operator-overloading overload-resolution name-lookup
C++编译器有没有办法在运算符重载解析期间执行隐式转换?
例如,我希望以下示例中的最后一行以与前一行相同的方式进行编译和行为。
struct A
{
private:
int _val;
public:
A ( int val ) : _val( val ) {}
A operator+( A a ) { return _val + a._val; }
};
struct B
{
private:
int _val;
public:
B ( int val ) : _val( val ) {}
operator A () { return _val; }
};
int main()
{
B b1 = { 0 }, b2 = { 1 };
auto a = static_cast<A>( b1 ) + b2; // Works …Run Code Online (Sandbox Code Playgroud) 考虑以下代码。
struct Widget {
int& get();
};
template<typename X>
auto func_1(X& x) {
return x.get();
}
template<typename X>
auto func_2(X& x) -> decltype(x.get()) {
return x.get();
}
Run Code Online (Sandbox Code Playgroud)
当使用 type 的左值调用时Widget,该函数func_1将使用返回类型进行实例化int,其中函数func_2将具有返回类型int&。
func_1另外,和之间存在差异,func_2因为“表达式 SFINAE”是针对 执行的func_2。因此,对于X没有.get()成员的类型,func_2将不会参与重载决策。
我的问题是:我们如何func_1在执行表达式 SFINAE 的同时获得返回类型行为?
以下func_3似乎适用于我测试的情况,但我觉得应该有一个更简单的替代方案。另外,我不确定是否func_3具有与所有情况下完全相同的返回类型func_1。
template<typename X>
auto func_3(X& x) -> std::remove_cvref_t<std::decay_t<decltype(x.get())>> {
return x.get();
}
Run Code Online (Sandbox Code Playgroud) 在下面的程序中,为什么编译器会为调用printMax模板函数而不是调用函数生成错误printMaxInts?
#include <iostream>
template<class A>
void printMax(A a,A b)
{
A c = a>b?a:b;
std::cout<<c;
}
void printMaxInts(int a ,int b)
{
int c = a>b?a:b;
std::cout<<c;
}
int main()
{
printMax(1,14.45);
printMaxInts(1,24);
}
Run Code Online (Sandbox Code Playgroud) 为什么以下重载函数调用不明确?编译错误:
调用重载'test(long int)'是模棱两可的,候选者是:void test(A)| 无效测试(B)|
代码:
class A
{
public:
A(int){}
A(){}
};
class B: public A
{
public:
B(long){}
B(){}
};
void test(A a)
{
}
void test(B b)
{
}
void main()
{
test(0L);
return;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试重载
int foo(int,int);
int foo(int);
int main() {
//decltype(foo) x; // error: reference to overloaded function could not be resolved;
decltype(foo(1)) f;
}
Run Code Online (Sandbox Code Playgroud)
我知道foo除非我选择其中一个重载,否则我无法获得指针,但我想知道为什么重载集没有类型?写作并不自然
template <typename F>
void foofoo(F f,bool x) {
if (x) f(1);
else f(1,0);
}
Run Code Online (Sandbox Code Playgroud)
并称之为foofoo(foo,true);?我知道有不同的方法来编写类似的东西,但我特别想知道重载集.无论如何,我的问题是
在选择其中一个重载之前,我是否可以对重载设置做些什么?
换句话说,是有一些magic,这将使以下true字面上是什么?
std::is_same_v< decltype( foo(1,1) ), magic<foo,int,int>::type >;
Run Code Online (Sandbox Code Playgroud)
或者这通常是不可能的,因为foo尚未解决?
考虑以下程序:
\n#include <iostream>\n#include <cstring>\nusing namespace std;\n\nvoid print(const char *pa) { \n cout << "Print - using pointer" << endl;\n}\n\nvoid print(const char (&arr)[]) {\n cout << "Print - using reference" << endl;\n}\n\nint main() { \n char ca[] = {'B', 'A', 'D', 'C', '\\0'};\n print(ca);\n}\n\nResults:\n\nPrint - using reference\nRun Code Online (Sandbox Code Playgroud)\n为什么引用优于指针?
\n根据 C++ Primer 第 5 版,第 6.6.1 节:
\n为了确定最佳匹配,编译器会对\n可用于将每个参数转换为其相应参数类型的转换进行排名。转化排名如下:
\n\n
- 完全匹配。当出现以下情况时,会发生完全匹配:
\n
\n\xe2\x80\xa2 参数和形参类型相同。
\n\xe2\x80\xa2 参数从数组或函数类型转换为相应的指针类型。(\xc2\xa7 6.7 (p. 247) 涵盖了函数指针。)
\n\xe2\x80\xa2 顶级常量被添加到参数中或从参数中删除。- 通过 const 转换进行匹配(\xc2\xa7 4.11.2,第 162 页)。 …
// overloads
void f(int x){}
void f(float x){}
// generic
void s<T>(T t){
f(t); // <<< cannot convert from 'T' to 'int'
}
// use
s(10);
Run Code Online (Sandbox Code Playgroud)
C# 编译器响应说,在 的正文中s<T>, I cannot convert from 'T' to 'int'。还有另一种方法可以弥补通用 -> 过载差距吗?
(我正在使用 C# 10)
考虑以下代码:
void foo(int* = 0); // (1)
void foo(int = 0); // (2)
int main() {
foo(0); // OK, calls (2)
foo(); // error, ambiguous overload
}
Run Code Online (Sandbox Code Playgroud)
GCC 和 clang 都是这样的,但我不相信它是正确的。
[expr.call] p6只是说:
如果没有相应的参数,则使用参数的默认参数。
这表明,一旦进行函数调用,foo将变为foo(0)且0必须将其转换为int*for (1)。
因此,我相信这两个函数调用应该是有效的。然而,有没有与这个想法相矛盾的措辞呢?默认参数的转换是否被视为“自由”,以便它们不会对转换序列产生影响?
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int square(int a){
return a*a;
}
struct Point{
int x,y;
};
int distance (const Point& a,const Point& b){
int k=(int) sqrt((float)((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y)));
return k;
}
int main(){
vector<Point>a(10);
for (int i=0;i<10;i++){
cin>>a[i].x>>a[i].y;
}
int s=0;
int s1;
int k=0;
for (int i=1;i<10;i++){
s+=square(distance(a[0],a[i]));
}
for (int i=1;i<10;i++){
s1=0;
for (int j=0;j<10;j++){
s1+=square(distance(a[i],a[j]));
if (s1<s) { s=s1; k=i;}
}
}
cout<<k<<"Points are:";
cout<<a[k].x;
cout<<a[k].y;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我有以下代码,但这里是错误列表
1>------ Build started: Project: distance, …Run Code Online (Sandbox Code Playgroud) c++ compiler-errors using-directives overload-resolution name-lookup