相关疑难解决方法(0)

标准库如何实现std :: swap?

如何在STL中实现交换功能?它是这么简单:

template<typename T> void swap(T& t1, T& t2) {
    T tmp(t1);
    t1=t2;
    t2=tmp;
}
Run Code Online (Sandbox Code Playgroud)

在其他帖子中,他们谈论为您自己的班级专门化这个功能.我为什么要这样做?为什么我不能使用这个std::swap功能?

c++ templates swap

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

为什么B :: f不解决歧义,但A :: f呢?

为什么B :: f不解决歧义,但A :: f呢?

namespace A
{
    class X { };
    void f( X );
} 

namespace B
{
    void f( A::X );
    void g( A::X x )
    {
        using B::f;   // which expression shall I use here to select B::f?
        f(x);         // ambiguous A::f or B::f
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ argument-dependent-lookup

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

为什么GCC允许在不使用其命名空间的情况下调用此函数?

可能重复:
为什么C++参数范围会影响命名空间中的函数查找?

今天我经历了这种奇怪的行为.我可以先调用strangeFn using namespace Strange,但不允许调用strangeFn2为什么?

namespace Strange
{
    struct X
    {
    };
    void strangeFn(X&) {}
    void strangeFn2(int) {}
}

int main()
{
    Strange::X x;
    strangeFn(x);    // GCC allows calling this function.
    strangeFn2(0);   // Error: strangeFn2 is not declared in this scope.
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

C++编译器如何解决符号的范围?

c++ scope namespaces

17
推荐指数
1
解决办法
1016
查看次数

内部函数的ranged错误

我在C++中使用ranged有点麻烦.我正在尝试使用它来显示元素和int数组(int []),当我在main函数上执行它时,它完全正常,如:

int main(int argc, char const *argv[]) {

  int v[] = {3, 4, 6, 9, 2, 1};

  for (auto a : v) {
      std::cout << a << " ";
  }
  std::cout << std::endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到了我想要的和预期的输出,即:

3 4 6 9 2 1
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在函数内部使用ranged时,事情变得有点奇怪,例如我遇到了这段代码的问题:

void printList(int *v);

int main(int argc, char const *argv[]) {

  int v[] = {3, 4, 6, 9, 2, 1};

  printList(v);

  return 0;
}

void printList(int *v) {
  for (auto a : v) {
    std::cout << …
Run Code Online (Sandbox Code Playgroud)

c++ arrays ranged-loops

14
推荐指数
3
解决办法
691
查看次数

具有相同参数的命名空间中的函数和函数之间的歧义

任何人都可以解释为什么A::f(const B& b)和之间存在歧义f(const A::B& b)。我认为代码对意图非常明确。

#include <iostream>

namespace A
{
  class B
  {
  protected:
    double value_;
  public:
    B() : value_(15.0) {}
    double getValue() const {return value_;}
  };

  void f(const B& b)
  {
    std::cout << "f(b) = " << b.getValue() << std::endl;
  }
}

void f(const A::B& b)
{
  std::cout << "Other f(b) = " << b.getValue() << std::endl;
}

int main()
{
  A::B b;
  A::f(b);
  f(b);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

然而,error: call of overloaded ‘f(A::B&)’ is …

c++ namespaces function-call argument-dependent-lookup

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

std :: vector <T>的比较运算符无法找到T的比较运算符

以下非常简单的代码将无法编译

#include <vector>
#include <string>


namespace Foobar {
    struct Test {
        std::string f;
        std::uint16_t uuid;
    };
}

bool operator==(const Foobar::Test& lhs, const Foobar::Test& rhs){
    return lhs.f == rhs.f && lhs.uuid == rhs.uuid;
}


int main(){

    std::vector<Foobar::Test> a;
    std::vector<Foobar::Test> b;

    if(a==b){

    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

https://godbolt.org/g/zn6UgJ

不会编译我的任何编译器.

而以下

#include <vector>
#include <string>


namespace Foobar {
    struct Test {
        std::string f;
        std::uint16_t uuid;
    };

    bool operator==(const Foobar::Test& lhs, const Foobar::Test& rhs){
        return lhs.f == rhs.f && lhs.uuid == rhs.uuid;
    }
}



int …
Run Code Online (Sandbox Code Playgroud)

c++ dependent-name template-function name-lookup argument-dependent-lookup

13
推荐指数
1
解决办法
371
查看次数

为什么ADL不能使用Boost.Range?

考虑到:

#include <cassert>
#include <boost/range/irange.hpp>
#include <boost/range/algorithm.hpp>

int main() {
    auto range = boost::irange(1, 4);
    assert(boost::find(range, 4) == end(range));
}
Run Code Online (Sandbox Code Playgroud)

Live Clang demo Live GCC demo

这给了:

main.cpp:8:37:错误:使用未声明的标识符'end'

考虑到如果你写using boost::end;工作得很好,这意味着它boost::end是可见的:

为什么ADL不工作并且boost::end在表达中找到end(range)?如果它是故意的,它背后的理由是什么?


需要说明的是,预期结果与本例中使用std::find_if和不合格的结果类似end(vec).

c++ boost namespaces argument-dependent-lookup boost-range

12
推荐指数
3
解决办法
758
查看次数

在这种情况下,为什么编译器会选择不正确的函数重载?

我正在尝试Sean Parent在GoingNative 2013上的演讲中提出的代码 - "继承是邪恶的基础".(最后一张幻灯片的代码可在https://gist.github.com/berkus/7041546获得

我试图自己实现相同的目标,但我无法理解为什么下面的代码不会按照我的预期行事.

#include <boost/smart_ptr.hpp>
#include <iostream>
#include <ostream>

template <typename T>
void draw(const T& t, std::ostream& out)
{
    std::cout << "Template version" << '\n';
    out << t << '\n';
}

class object_t
{
public:
    template <typename T>
    explicit object_t (T rhs) : self(new model<T>(rhs)) {};

    friend void draw(const object_t& obj, std::ostream& out)
    {
        obj.self->draw(out);
    }

private:
    struct concept_t
    {
        virtual ~concept_t() {};
        virtual void draw(std::ostream&) const = 0;
    };

    template <typename T>
    struct model …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates language-lawyer argument-dependent-lookup

11
推荐指数
1
解决办法
744
查看次数

不合格的 sort()——为什么在 std::vector 上使用而不是在 std::array 上编译,哪个编译器是正确的?

当调用std::sort()a 时std::array

#include <vector>
#include <array>
#include <algorithm>

int main() {
    std::vector<int> foo{4, 1, 2, 3};
    sort(begin(foo), end(foo));

    std::array<int, 4> foo2{4, 1, 2, 3};
    sort(begin(foo2), end(foo2));
}
Run Code Online (Sandbox Code Playgroud)

gcc 和 clang 都在排序上返回错误std::array-- clang 说

错误:使用未声明的标识符“排序”;你的意思是'标准::排序'?

更改以std::sort(begin(foo2), end(foo2))解决问题。

MSVC 按照编写的方式编译上面的代码。

为什么std::vector和之间的待遇不同std::array;哪个编译器是正确的?

c++ c++17

11
推荐指数
1
解决办法
465
查看次数

命名空间或类,更适合封装功能成员

所以,假设我有一些函数来处理文件的打开/关闭.

是否更好地创建一个具有静态声明的所有这些函数的类,或者只是将"public"函数放在命名空间"file"的头文件中,并将其余的"实现细节"放在.cc文件中?

以下是代码示例.

命名空间一有点长,因为我想让它尽可能清晰.

谢谢!!


类实现

标题:

#ifndef FILE_H
#define FILE_H

#include <iostream>
#include <fstream>

include "common.h"

enum Errorcode {
    FILE_CANNOT_OPEN,
    FILE_CANNOT_CLOSE
};

class file {

public:
    static common::Lines toLines(std::string filename);

private:
    static void err(Errorcode e, std::string msg);
    static void toLines(std::ifstream &ifs, common::Lines &lines);

};

#endif
Run Code Online (Sandbox Code Playgroud)

.cc文件:

/*just the implementation details of above class.*/
Run Code Online (Sandbox Code Playgroud)

命名空间实现

标题:

#ifndef FILE_H
#define FILE_H

#include <iostream>
#include <fstream>

#include "common.h"

namespace file {

common::Lines toLines(std::string filename);

}

#endif    
Run Code Online (Sandbox Code Playgroud)

.cc文件:

namespace file {

enum Errorcode …
Run Code Online (Sandbox Code Playgroud)

c++ encapsulation namespaces class

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