标签: overload-resolution

使用泛型类型来调用重载函数

StreamWrite.Write目前超载Int16,Int32,Int64,Double,Single,String等等.

为什么我需要使用动态?当调用WriteList方法时,编译器知道它被调用Int32,String.......
那么为什么我不能直接使用e(类型T = Int32)StreamWrite.Write呢?

public void WriteList<T>(List<T> list) 
{
  int count = list.Count();
  StreamWriter.Write(count);
  foreach(T e in list) 
  {
    dynamic d = e;
    StreamWriter.Write(d);
  }
}
Run Code Online (Sandbox Code Playgroud)

c# generics list overload-resolution

3
推荐指数
1
解决办法
103
查看次数

SFINAE是否适用于职能机构?

我有以下示例代码:

class Serializable {};

class MyData : public Serializable {};

void GetData( Serializable& ) {}

template<typename T>
void GetData( T& data )
{
    std::istringstream s{"test"};
    s >> data;
}

int main()
{
    MyData d;
    GetData(d);
}
Run Code Online (Sandbox Code Playgroud)

现场样本

基于重载解析规则,应首选非模板版本,因为基类是type Serializable。但是,我希望在实例化模板版本以进行重载解析时,模板版本中有错误时,SFINAE会启动(因为如果未为类型定义>>运算符,则不应考虑)。

即使不使用模板,为什么仍然失败?

c++ templates upcasting language-lawyer overload-resolution

3
推荐指数
1
解决办法
170
查看次数

重载决议:这怎么不模糊?

假设我们有这个代码,从一个单独的问题复制:

namespace x 
{
    void f()
    {
    }

    class C 
    {
        void f() 
        {
            using x::f;
            f();         // <==
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

f指示行上的名称明确指出x::f(至少根据gcc和clang).为什么在这种情况下更x::f喜欢x::C::f?两个名字都可见,这不应该是模棱两可的吗?

c++ language-lawyer overload-resolution

3
推荐指数
2
解决办法
111
查看次数

为什么编译器无法在没有引用运算符的情况下决定调用哪个函数

下面你可以看到名为max的小函数.

template <typename T>
T max(T& arg1, T& arg2) {    
    if (arg1 > arg2) {
        return arg1;
    } else {
        return arg2;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在main中调用此函数时,它完全有效,但是如果我从参数中删除引用

T max(T arg1, T arg2)  
Run Code Online (Sandbox Code Playgroud)

然后编译器给出以下错误

main.cpp:18:21: error: call of overloaded 'max(int&, int&)' is ambiguous
Run Code Online (Sandbox Code Playgroud)

很明显,编译器无法决定是调用我的函数还是标准函数.这里的问题是如何在论证的头部有参考时如何决定和工作?

当我用恒定参数调用函数时

const int a = 12;
const int b = 24;
cout << max(a, b) << endl;
Run Code Online (Sandbox Code Playgroud)

它调用标准的max函数.但是当我使用非const值调用时,它会调用我的函数.

我可以理解,如果对象是const,那么const函数将在其他地方调用,将调用非const函数.但为什么它应该引用触发这种机制?为什么没有参考运算符就无法决定呢?

c++ templates overload-resolution

3
推荐指数
1
解决办法
101
查看次数

超负荷的呼唤是暧昧的

我正在学习新的C++语义,我在这个程序中遇到错误:

#include <iostream>
#include <string>
#include <utility>

std::string foo(std::string str)
{
    return str + " call from normal";
}

std::string foo(const std::string& str)
{
    return str + " call from normal";
}

std::string foo(std::string&& str)
{
     return str + " call from ref ref";
}

int main()
{
    std::string str = "Hello World!";
    std::string res = foo(str);
    std::string&& res_ref = foo(std::move(str));
    std::cout << "Res ref = " << res_ref << std::endl;
    std::cout << "Str = " << str << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ overload-resolution c++14

3
推荐指数
1
解决办法
1050
查看次数

如果有专门的函数和模板函数,为什么没有必要专门研究`std :: nullptr_t`

请考虑以下代码:

#include <iostream>
using namespace std;

void fun(const char* s){
    if (s == nullptr) {
        puts("const char* nullptr");
    } else {
        printf("%s\n", s);
    }
}

template <typename T>
void fun(T* p){
    printf("%p\n", p);
}

int main() {
    int a;
    fun("abc"); // Resolves to fun(const char*)
    fun(&a); // Specializes the template to int*
    fun(nullptr); // Uses fun(const char*)??
    fun(NULL); // Same as above
}
Run Code Online (Sandbox Code Playgroud)

我很诧异g++ 7.2.0不会抛出不明的重载解析错误,因为我认为nullptrNULL可以融入任何指针类型,其中包括fun(int*)专门从模板,只要不是专门用于过载std::nullptr_t.

为什么 …

c++ null nullptr language-lawyer overload-resolution

3
推荐指数
1
解决办法
113
查看次数

如何为C++模板完成功能解析?

困扰我的代码发布在这里:

namespace ns1 {
    struct myStruct1 {};
    struct myStruct2 {};
}

namespace ns2 {
    template <typename T>
    constexpr int foo(T& x) {
        return 1;
    }

    // If the two functions below are switched, it returns 2 correctly
    template <typename T>
    constexpr int fooCaller(T& x) {
        return foo(x);
    }

    constexpr int foo(ns1::myStruct2& x) {
        return 2;
    }
}

// If the below is uncommented, it also returns 2 correctly
/*
namespace ns1 {
    constexpr int foo(myStruct2& x) {
        return 2;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading function overload-resolution

3
推荐指数
1
解决办法
84
查看次数

模板和非模板函数之间的C++重载解析

我对模板和非模板函数之间的c ++重载解析感到困惑,以下是示例:

class Stream {};

struct M
{
    M(float v) {}
};

template <class T>
Stream& operator<<(Stream& stream, T& v) {}

Stream& operator<<(Stream& stream, const M& v) {}

int main()
{
    Stream stream;
    int a = 1;

    stream << a; // sample 1
    stream << a * a; // sample 2

    return;
}
Run Code Online (Sandbox Code Playgroud)

这里,示例1调用模板函数.示例2提供了一个int&&类型参数,可以隐式地转换const M&为非模板,而不是模板T = const int.

样本2的重载分辨率会发生什么?

c++ templates overload-resolution

3
推荐指数
1
解决办法
181
查看次数

为什么要使用int而不是bool的转换?

我为一个类实现了两次转换。一种是布尔,一种是int&。

如果我隐式转换为int,则使用int&转换,但是如果我想要布尔型,则仍使用int&转换。

struct A
{
    operator bool() const;
    operator int&();
};

// trying to call these:
A a;
int  i_test = a; // calls operator int&()
bool b_test = a; // calls operator int&()
Run Code Online (Sandbox Code Playgroud)

我知道a会隐式转换为int&然后转换为bool,但是为什么要花更长的时间呢?我如何不用写就可以避免它a.operator bool()

c++ overload-resolution implicit-conversion

3
推荐指数
1
解决办法
69
查看次数

当两个模板参数属于同一类型时,如何执行部分专业化?

两个模板参数为同一类型如何部分专业化。

如何使用第二个函数编写此代码。

#include <utility>
#include <iostream>

template <typename A, typename B>
void Translate(A&& a,B* b){
  // make some translate from a to b
  // b->bvalue=a.av;
  std::cout<<"normal function";
}
//if a and b are same type,
template <typename A>
void Translate(A&& a, A* b) {
  *b = std::forward<A>(a);
  std::cout<<"forward function";
}

int main(int argc, char** argv) {
  int in=0,out=0;
  Translate(in,&out);
  return 0;
}

Run Code Online (Sandbox Code Playgroud)

期望输出“转发功能”。

c++ templates partial-specialization specialization overload-resolution

3
推荐指数
1
解决办法
53
查看次数