相关疑难解决方法(0)

"using"关键字在c ++中究竟做了什么?

我发现关于"使用"关键字的一个令人困惑的事情.如果我使用类或结构,那么就没有必要在同一名称空间中使用函数,该函数将该类或结构作为参数.像下面的代码.

namespace A
{
    struct testData
    {
        int x;
    };

    int testFunc(testData data)
    {
        return data.x;
    }
}

#include <cstdio>;

using A::testData;

int main()
{
    testData test = { 1 };
    printf("%d", testFunc(test));

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

我以为我不应该被允许使用testFunc(),因为我只对testData使用"using"关键字.但是,这些代码工作得很好.

你能告诉我为什么这样做吗?

c++

9
推荐指数
1
解决办法
266
查看次数

尝试使用模板化函数来交换两个字符串

#include<iostream>
#include<string>

template <typename T>
void swap(T a , T b)
{
  T temp = a;
  a = b;
  b = temp;
}

template <typename T1>
void swap1(T1 a , T1 b)
{
  T1 temp = a;
  a = b;
  b = temp;
}

int main()
{
  int a = 10 , b = 20;
  std::string first = "hi" , last = "Bye";

  swap(a,b);
  swap(first, last);   

  std::cout<<"a = "<<a<<" b = "<<b<<std::endl;
  std::cout<<"first = "<<first<<" last = "<<last<<std::endl;    

  int c …
Run Code Online (Sandbox Code Playgroud)

c++ templates pass-by-reference pass-by-value stdstring

9
推荐指数
1
解决办法
232
查看次数

make_pair命名空间污染

在我最近写的代码中,我注意到了一种奇怪的行为.

当我使用make_pair第一个参数为a时std::pair,make_pair在命名空间中变得"神奇地"可用(我不必使用std::限定符)

#include <iostream>

int main()
{   
    int i1 = 2; int i2 = 10; int i3 = 0;

    // constructing a pair using std::make_pair, everything's okay
    std::pair<int,int> key = std::make_pair(i1, i2);

    // here, why is make_pair suddenly magically available without the
    // std:: namespace qualifier?
    // At the same time, using ::make_pair yields and error
    // (make_pair has not declared...)
    std::pair<std::pair<int,int>, int> mypair = make_pair(key, i3);

    std::cout << mypair.first.first << "\n"; …
Run Code Online (Sandbox Code Playgroud)

c++ gcc stl std-pair

8
推荐指数
1
解决办法
476
查看次数

g++ std::visit 泄漏到全局命名空间?

我只是在 std::visit 和 std::function 附近反弹到一些微妙的东西,这让我感到困惑。我并不孤单,但我能找到的唯一其他人做了“解决方法并继续前进”的舞蹈,这对我来说还不够:

这可能与 LWG 中的一个悬而未决的问题有关,但我认为这里正在发生一些更险恶的事情:

最小示例:

// workaround 1: don't include <variant>
#include <variant>
#include <functional>

struct Target
{
  Target *next = nullptr;
};

struct Visitor
{
  void operator()(const Target &tt) const { }
};

// workaround 2: concretely use 'const Visitor &' instead of 'std::function<...>'
void visit(const Target &target, const std::function<void(const Target &)> &visitor)
{
  visitor(target);
  if(target.next)
    visit(*target.next,visitor); // workaround 3: explicitly invoke ::visit(...)
    //^^^ problem: compiler is trying to resolve this as …
Run Code Online (Sandbox Code Playgroud)

c++ g++ visitor variant c++17

8
推荐指数
1
解决办法
207
查看次数

C++中依赖于参数的查询

这是如何运作的?它与ADL有关吗?

#include <iostream>

template <typename T>
struct A
{
    friend void f(T x)
    {
        std::cout << "A\n";
    }
};

int main()
{
    f(new A<void*>());
}
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我为什么我不能使用类似的东西

f(A<int>());
Run Code Online (Sandbox Code Playgroud)

c++ argument-dependent-lookup

7
推荐指数
1
解决办法
2407
查看次数

C++名称解析

我想知道一点关于namespaceusingC++基本上我想知道差异并弄清楚如何以最好的方式使用它.

我看到它有(至少)三种方法来解决一个类名,我不知道如何选择它们:

  1. using namespace <namespace>
  2. using <namespace>::<what_to_use>
  3. <namespace>::<what_to_use> <use_it>

我想知道优势,特别是如果有一种或另一种方式涉及性能,如果它只是语法和偏好的问题,或者是否有其他事情我没有考虑过这个.

c++ namespaces using

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

我真的需要向朋友操作符<<向后弯曲吗?对于命名空间中的类?

我想实现一个运算符<< for streaming my class(比如说它的名字Paragraph).类Paragraph有一些私有数据,因此我希望(独立)运算符<<函数成为朋友.所以,我按照建议做,例如,在这里.friend声明,实施operator<<和一切都很好.

但是现在我想将Paragraph放在命名空间中namespace foo.它不再有效!如果我写:

namespace foo {
class Paragraph {
    public:
        explicit Paragraph(std::string const& init) :m_para(init) {}
        std::string const&  to_str() const { return m_para; }
    private:
        friend std::ostream & operator<<(std::ostream &os, const Paragraph& p);
        std::string     m_para;
};
} // namespace foo
Run Code Online (Sandbox Code Playgroud)

编译器告诉我,我已成为朋友foo::operator<<,而不是::operator<<.好,可以.所以,我用以下内容替换了朋友行:

    friend std::ostream & ::operator<<(std::ostream &os, const Paragraph& p);
Run Code Online (Sandbox Code Playgroud)

但我再次收到错误(来自GCC 5.4.0),告诉我::operator<<尚未宣布.好的,我们先声明吧.这有用吗?:

namespace foo {
std::ostream & ::operator<<(std::ostream &os, …
Run Code Online (Sandbox Code Playgroud)

c++ encapsulation private friend

7
推荐指数
1
解决办法
133
查看次数

boost :: lexical_cast无法识别重载的istream运算符

我有以下代码:

#include <iostream>
#include <boost\lexical_cast.hpp>

struct vec2_t
{
    float x;
    float y;
};

std::istream& operator>>(std::istream& istream, vec2_t& v)
{
    istream >> v.x >> v.y;

    return istream;
}

int main()
{
    auto v = boost::lexical_cast<vec2_t>("1231.2 152.9");

    std::cout << v.x << " " << v.y;

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

我从Boost收到以下编译错误:

错误1错误C2338:目标类型既不是std :: istream able nor std::wistream能够的

这看起来很简单,过去一小时我一直在桌子上打我的头.任何帮助,将不胜感激!

编辑:我正在使用Visual Studio 2013.

c++ boost operator-overloading istream lexical-cast

6
推荐指数
1
解决办法
1505
查看次数

对std/boost移动的模糊调用

碰到这个不编译的代码:

#include <boost/move/utility.hpp>
#include <utility>
#include <deque>
#include <map>
#include <vector>
#include <boost/date_time/posix_time/posix_time_types.hpp>

using namespace std;

int main() {
    typedef std::pair<int, std::deque<int>> FirstPair;
    typedef std::vector<FirstPair> VectorFirstPair;
    typedef std::pair<boost::posix_time::time_duration, VectorFirstPair> SecondPair;
    typedef std::map<boost::posix_time::time_duration, SecondPair> Map;
    Map mapInstance;
    SecondPair newElement = make_pair(boost::posix_time::not_a_date_time, VectorFirstPair());
    mapInstance.insert(make_pair(boost::posix_time::seconds(10), move(newElement))).first;
}
Run Code Online (Sandbox Code Playgroud)

这在使用boost 1.55(不是在boost 1.54上)的gcc 4.8.2上失败,并出现以下错误(此处为 ideone ):

test.cpp: In function ‘int main()’:
test.cpp:17:81: error: call of overloaded ‘move(SecondPair&)’ is ambiguous
     mapInstance.insert(make_pair(boost::posix_time::seconds(10), move(newElement))).first;
                                                                                 ^
test.cpp:17:81: note: candidates are:
In file included from /usr/include/c++/4.8/bits/stl_pair.h:59:0,
                 from /usr/include/c++/4.8/utility:70,
                 from /usr/include/boost/config/no_tr1/utility.hpp:21, …
Run Code Online (Sandbox Code Playgroud)

c++ boost

6
推荐指数
1
解决办法
998
查看次数

代码未使用特定名称函数编译

这段代码不能在我的电脑上编译(用最少的例子编辑):

// Example program
#include <iostream>
#include <string>
#include <vector>
#include <tuple>

template<class PValues>
inline bool equal(PValues it,
                  PValues last,
                  size_t  cols,
                  size_t  rows,

                  size_t offset)
{
  if(std::distance(it,last)<offset)
    return false;

  for(size_t c=0; c<cols; ++c)
    {
      auto p = it + c*rows;
      if(*p != *(p + offset))
        return false;
    }

  return true;
};



int main()
{
  std::vector<std::string> vec = {"1","1","1","2","2","2","3","8","8",
                                  "5","5","2","5","5","5","6","8","8",
                                  "3","3","3","4","4","3","9","8","8"};
  std::vector<double> nbs = {1,2,3,4,5,6,7,8,9};

  auto res = equal(vec.data(), vec.data()+9, 3, 9, 1);
}
Run Code Online (Sandbox Code Playgroud)

我有以下错误:

In file included from /usr/include/c++/9/bits/char_traits.h:39, …
Run Code Online (Sandbox Code Playgroud)

c++ g++

6
推荐指数
1
解决办法
79
查看次数