如此处所述,C++11 样式 SFINAE 和模板实例化类成员函数上的函数可见性掩盖了自由函数。使用完全限定名称通常有效,但是我很难使用其他内嵌声明的类的友元函数。考虑以下示例:
namespace N {
struct C {
friend int f(const C& c) {
return 1;
}
friend int g(const C& c) {
return 2;
}
};
struct D {
void f() {
g(C{}); // ADL finds this
::N::f(C{}); // not found dispite full qualification
}
};
}
Run Code Online (Sandbox Code Playgroud)
我想我明白问题是什么,正如这里描述的内联友元函数的范围是什么?内联友元函数通常使用 ADL 找到,并且在封闭的命名空间中并不真正可见。
所以我的问题是我应该如何更改我的代码以使其工作(除了重命名 f 之一)?
如果我有一个struct A定义为:
struct A {
const char *data;
operator const char * () const { return data; }
friend bool operator== (const A &s1, const char *s2)
{ return /* typical string comparison result */; }
};
Run Code Online (Sandbox Code Playgroud)
而我写的A{"hello"} == "test2",A::operator==叫什么?标准中的内容是什么(为什么不A隐式转换为const char *?)
怎么样"test2" == A{"hello"}?A在这种情况下是否已转换?
编辑:如果struct A也有成员呢:
friend bool operator== (const char *s1, const A &s2)
Run Code Online (Sandbox Code Playgroud) c++ operator-overloading overload-resolution implicit-conversion
有谁知道如何查看 Visual Studio 2019 mac Intellisense 中的多重重载?对于这个主题,谷歌搜索显示为空白。
我只是想找出如何滚动智能感知弹出窗口上的重载(即使存在多个重载,也没有向上/向下箭头),请参阅屏幕截图,看到它显示+3重载,但没有办法看到它们是什么:
找不到任何关于此的内容。谢谢。
试图向 SFINAE 说再见。
是否可以使用concepts来区分函数,以便编译器可以根据发送的参数是否满足concept约束来匹配正确的函数?
例如,重载这两个:
// (a)
void doSomething(auto t) { /* */ }
// (b)
void doSomething(ConceptA auto t) { /* */ }
Run Code Online (Sandbox Code Playgroud)
因此,当被调用时,编译器会在每次调用时匹配正确的函数:
doSomething(param_doesnt_adhere_to_ConceptA); // calls (a)
doSomething(param_adheres_to_ConceptA); // calls (b)
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用max函数返回最大值,但它不适用于 3 个值。
代码块错误:
错误:“__comp”不能用作函数
编码:
#include <iostream>
using namespace std;
int main()
{
cout << max(5, 10, 20);
}
Run Code Online (Sandbox Code Playgroud) 考虑这个示例程序:
#include <iostream>
typedef enum { A, B, C } MyEnum;
struct S
{
S(int) { std::cout << "int" << std::endl; }
S(MyEnum) { std::cout << "MyEnum" << std::endl; }
};
S f()
{
return A;
}
int main()
{
S const s = f();
}
Run Code Online (Sandbox Code Playgroud)
使用 clang 和 gcc 编译,生成一个可执行文件,在运行时打印“MyEnum”。C++ 标准保证这种行为吗?
我有一个可变参数函数,我想在第一个参数类型上重载.
void write( void ) { }
void write( std::ostream& ) { }
template< typename Head, typename... Rest >
void write( std::ostream& out, Head&& head, Rest&&... rest )
{
out << head;
write( out, std::forward<Rest>(rest)... );
}
template< typename... Args >
void write( Args&&... args )
{
write( std::cout, std::forward<Args>(args)... );
}
Run Code Online (Sandbox Code Playgroud)
但是这些功能并不像预期的那样.
write( "## to cout ##" ); // printed to stdout as expected
write( std::cerr, "## to cerr ##" ); // printed to stderr as expected
std::ostringstream oss; …Run Code Online (Sandbox Code Playgroud) 我今天遇到了一个奇怪的情况,声明一个带有某些参数的已删除运算符改变了看似无关的代码的行为.
我将它减少到以下.从这开始:
namespace N
{
enum E { A, B };
struct C
{
C(E);
private:
C(int);
};
}
N::E operator|(N::E, N::E);
namespace N
{
void Waldo()
{
C(A | B);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,C有两个构造函数,一个是公共的,另一个是私有的.此代码编译,表明正在选择公共重载,因此表达式A | B具有类型E.反过来,这也意味着,operator|(N::E, N::E)已被匹配(否则A和B将接受隐式转换为整数的类型,A | B会int和私人构造函数相匹配.
到现在为止还挺好.现在我定义一个新的枚举类型F,并删除operator|涉及F:
namespace N
{
enum E { A, B };
struct C
{
C(E);
private:
C(int);
};
}
N::E operator|(N::E, N::E);
namespace N …Run Code Online (Sandbox Code Playgroud) 鉴于此代码:
template< class C >
void foo( C const& o ) { o.nosuch(); }
struct Base {};
void foo( Base const& ) {}
struct Derived: Base {};
auto main() -> int
{
Derived d;
foo( d ); // !Invokes template.
}
Run Code Online (Sandbox Code Playgroud)
...我希望调用调用Base为其定义的重载,而不必为其定义重载或模板特化Derived.
目标是能够应用于foo所有类型的对象,而不仅仅是Base(和Derived)对象,具有大多数对象的通用实现.
如果答案确切地解释了在这种情况下重载解析如何工作,以及它如何与Derived定义的重载一起工作,那也是很好的.
在出现此问题的代码中,foo上面是一个n_items定义如下的函数模板:
template< class Type >
auto n_items( Type const& o )
-> size_t
{ return o.size(); }
template< size_t n …Run Code Online (Sandbox Code Playgroud) #include <iostream>
struct A
{
void update(bool const & v)
{
std::cout << std::boolalpha << v << std::endl;
}
void update(std::string_view v)
{
std::cout << v << std::endl;
}
};
template <typename T>
void update(T const & item)
{
A a;
a.update(item);
}
int main()
{
const char * i = "string";
update(i);
}
Run Code Online (Sandbox Code Playgroud)
当我用a调用update时const char *,编译器使用bool参数而不是string_view?!调用函数。为什么??
c++ ×9
c++11 ×2
c++-concepts ×1
c++17 ×1
c++20 ×1
enums ×1
friend ×1
intellisense ×1
max ×1
namespaces ×1
overloading ×1
sfinae ×1