标签: overload-resolution

c ++运算符重载参数与普通参数

我有一个功能模板(c ++)

template<typename T>
void print_to_default_file(T &obj, ADDON addon = "")
Run Code Online (Sandbox Code Playgroud)

和一个重载的功能

template<typename T>
void print_to_default_file(T &obj, std::string  objS) // or char* objS
Run Code Online (Sandbox Code Playgroud)

该类ADDON具有以下签名的运算符重载

void operator=(const std::string)
Run Code Online (Sandbox Code Playgroud)

问题是,当我执行print_to_default_file("测试","我将去哪里")

它正在呼叫第一个,但我想打电话给第二个.我也厌倦了char*而不是std :: string但结果是一样的.

有人可以指出出了什么问题

ADDON简化版

class ADDON {
    std::string s;

public:
    ADDON() {
        s = "";
    }

ADDON(std::string in) {
    s = in;
}

    ADDON(const char in[]) {
        s = in;
    }

    void operator=(const std::string in) {
        s = in;
    }

    std::string getString() {
        return s; …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading overload-resolution implicit-conversion

2
推荐指数
1
解决办法
137
查看次数

重载解析如何在私有修饰符的上下文中工作?

我无法理解以下 C++ 代码段的输出。

不应该objc.fn()调用A's,fn()因为 Bfn()private并且不应该在C. 然而,答案是:调用fn()不明确。如何?

#include<iostream>
using namespace std;

class A{
  public:
   void fn() { cout << "1"; }
};
class B{
   void fn() { cout << "2"; }
};
class C: public A, public B {};

int main(){
  C objc;
  objc.fn();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ inheritance overload-resolution

2
推荐指数
1
解决办法
369
查看次数

在正确实现 Scott Meyer 的更有效的 C++ 第 22 条:“考虑使用 op= 而不是独立 op”时避免通用引用?

我正在尝试遵循 Scott Meyers 在更有效的 C++ 第 22 条中的建议:“考虑使用op=而不是独立op”。他建议我们可以创建一个模板operator+,以便所有实现的类operator+=自动获得operator+

template<class T>
const T operator+(const T& lhs, const T& rhs)
{ 
    return T(lhs) += rhs; 
}
Run Code Online (Sandbox Code Playgroud)

现在,在《Effective Modern C++》第 25 条中,有一个矩阵加法的示例(第 172 页),其中operator+建议使用右值重载 ,因为如果您知道lhsrhs是右值,则可以使用它们来存储结果,从而防止可能巨大的矩阵的无用副本。所以我添加了重载:

template<class T>
T operator+(T&& lhs, const T& rhs)
{ 
    return std::move(lhs += rhs);
}

template<class T>
T operator+(T const& lhs, T&& rhs)
{ 
    return std::move(rhs += lhs);
}

template<class T>
T operator+(T&& …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading effective-c++ overload-resolution

2
推荐指数
1
解决办法
463
查看次数

在c ++中使用重载的歧义错误

我试图使用浮动和整数重载.当我只使用整数时,代码运行正常,但是当我包含浮动时它给了我错误.代码如下:

#include <iostream>
using namespace std;

int calculate(int x,int y);
float calculate(float x,float y);
const int MAININT=4;

int main()
{
    int result=calculate(5,5);
    float fresult=calculate(7.5,7.5);                 LINE X
    cout << (result + MAININT + fresult);             LINE Y
    return 0;
}

int calculate(int x,int y)
{
    int result=x*y;
    return result;
}

float calculate(float x,float y)
{
    int result=x*y;
    return result;
}
Run Code Online (Sandbox Code Playgroud)

通过从LINE Y删除LINE X和fresult,代码没有给我任何错误.所以我假设LINE X中肯定有问题,但我不明白为什么会出错.

我收到的错误消息是:

[Error] call of overloaded 'calculate(double, double)' is ambiguous
[Note] candidates are:
[Note] int calculate(int, int)
[Note] float …
Run Code Online (Sandbox Code Playgroud)

c++ overloading overload-resolution

2
推荐指数
1
解决办法
494
查看次数

不能调用模板构造函数

我正在实现类似于std :: list的类。我在调用错误的构造函数时遇到问题。

这是一个工作代码段:

#include <iostream>

template <typename T>
class dslist
{
public :
    typedef size_t    size_type ;
public :
    explicit dslist( const size_type count , const T &value ) ;
    template <typename InputIt>
    explicit dslist( InputIt first , InputIt last ) ;
} ;
template <typename T>
dslist<T>::dslist( const size_type count , const T &value )
{
    std::cout << "count, value ctor" << std::endl ;
}
template <typename T>
template <typename InputIt>
dslist<T>::dslist( InputIt first , InputIt last ) …
Run Code Online (Sandbox Code Playgroud)

c++ templates overload-resolution

2
推荐指数
1
解决办法
71
查看次数

强制 C++ 更喜欢带有隐式转换的重载而不是模板

我有一种情况,我需要重载解析来更喜欢带有隐式转换的重载而不是同名的模板函数。

考虑以下示例:

#include <iostream>
#include <functional>

void call_function(const std::function<void()>& function)
{
   std::cout << "CALL FUNCTION 1" << std::endl;
   function();
}

template <typename Function>
void call_function(const Function& function)
{
    std::cout << "CALL FUNCTION 2" << std::endl;
    function();
}

int main()
{
    // Pass a lambda to "call_function"
    // This lambda is implicitly convertible to 'std::function<void()>'
    // Even though it is implicitly convertible, the template function is selected by the compiler.
    call_function([]{
        std::cout << "TEST" << std::endl;
    });
}
Run Code Online (Sandbox Code Playgroud)

输出:

CALL FUNCTION …
Run Code Online (Sandbox Code Playgroud)

c++ templates overload-resolution implicit-conversion c++11

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

为什么 const 临时绑定到右值引用参数?

我有以下功能:

void func(void * const &ptr)
{
    std::cerr << "const" << std::endl;
}

void func(void *      &&ptr)
{
    std::cerr << "mutable" << std::endl;
}

void* const func2()
{
    return nullptr;
}
Run Code Online (Sandbox Code Playgroud)

一个重载采用 const 引用参数,另一个采用可变右值引用。并且有一个返回 const 值的函数。

当我将该 const 临时值传递给函数时:

func(func2());
Run Code Online (Sandbox Code Playgroud)

我希望选择 const 重载。但相反,我得到:

mutable
Run Code Online (Sandbox Code Playgroud)

这怎么可能?为什么 const 返回值绑定到非 const rvalue 引用参数?

然而,当void*我将 const 传递struct给函数时,这不会发生:

struct A
{
};

void func(A const &a)
{
    std::cerr << "const" << std::endl;
}

void func(A      &&a)
{
    std::cerr << "mutable" << …
Run Code Online (Sandbox Code Playgroud)

c++ rvalue overload-resolution

2
推荐指数
1
解决办法
83
查看次数

在模板化之前使用隐式转换

我尝试结合使用模板专业化和隐式转换,将模板类型分为 3 类:

  1. 可以隐式转换为字符串的类型
  2. 可以隐式转换为 double 的类型
  3. 不能隐式转换为字符串或双精度类型

然而,在我的这次尝试中,所有可以转换为双精度型或字符串的类型最终都以类型 T 的默认情况结束。

template<>
void Foo(std::string s)
{
    //can be converted to a string
}

template<>
void Foo(double d)
{
    //can be converted to a double
}

template<typename T>
void Foo(T t)
{
    //cannot be converted to a string or a double 
}

struct Other {};

int main()
{
    Foo("bar");// creates Foo(const char*) instead of converting to string
    Foo(1);// creates Foo(int) instead of converting to double
    Other o
    Foo(o);// creates Foo(Other)
} …
Run Code Online (Sandbox Code Playgroud)

c++ template-specialization overload-resolution

2
推荐指数
1
解决办法
92
查看次数

如何让编译器选择非成员函数重载

我正在编写一个库,它对内置类型(int、float、double 等)和用户提供的类型执行一些操作。其中之一是由模板函数执行的:

namespace lib
{
template<typename T>
inline auto from_string(std::string const & s, T & t) -> bool
{
    std::istringstream iss(s);
    iss >> t;
    return !iss.fail();
}
}
Run Code Online (Sandbox Code Playgroud)

这是一个定制点 - 用户可以根据自己的类型重载此函数:

namespace foo
{
class UserType
{
    // (...)
};
}

namespace lib
{
inline auto from_string(std::string const & s, foo::UserType & ut) -> bool
{
    // some implementation
}
}
Run Code Online (Sandbox Code Playgroud)

或者将from_string函数放在同一命名空间中并通过 ADL 访问:

namespace foo
{
inline auto from_string(std:string const & s, UserType & ut) -> bool …
Run Code Online (Sandbox Code Playgroud)

c++ overload-resolution c++17

2
推荐指数
1
解决办法
91
查看次数

为什么这个函数调用没有拒绝不合适的重载?

考虑以下代码:

#include<vector>
#include<ranges>
#include<algorithm>
//using namespace std;
using namespace std::ranges;
int main()
{
    std::vector<int> a = {};
    sort(a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它运行正常。

显然,它调用了这个重载函数(函子,严格来说):

template<random_access_range _Range,
     typename _Comp = ranges::less, typename _Proj = identity>
  requires sortable<iterator_t<_Range>, _Comp, _Proj>
  constexpr borrowed_iterator_t<_Range>
  operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  {
return (*this)(ranges::begin(__r), ranges::end(__r),
           std::move(__comp), std::move(__proj));
  }
Run Code Online (Sandbox Code Playgroud)

但是在我们引入命名空间 std 后,函数调用变得不明确(出现编译错误):

#include<vector>
#include<ranges>
#include<algorithm>
using namespace std;
using namespace std::ranges;
int main()
{
    std::vector<int> a = {};
    sort(a);
    return 0;
} …
Run Code Online (Sandbox Code Playgroud)

c++ overloading sfinae overload-resolution argument-dependent-lookup

2
推荐指数
1
解决办法
90
查看次数