标签: using-directives

在C++中使用指令与使用声明交换

请参考以下代码:

#include <algorithm>

namespace N
{

    template <typename T>
    class C
    {
    public:
        void SwapWith(C & c)
        {
            using namespace std; // (1)
            //using std::swap;   // (2)
            swap(a, c.a);
        }
    private:
        int a;
    };

    template <typename T>
    void swap(C<T> & c1, C<T> & c2)
    {
        c1.SwapWith(c2);
    }

}

namespace std
{

    template<typename T> void swap(N::C<T> & c1, N::C<T> & c2)
    {
        c1.SwapWith(c2);
    }

}
Run Code Online (Sandbox Code Playgroud)

如上所述,代码无法在Visual Studio 2008/2010上编译.错误是:

'void N::swap(N::C<T> &,N::C<T> &)' : could not deduce template argument for 'N::C<T> &' …
Run Code Online (Sandbox Code Playgroud)

c++ swap using-directives using-declaration

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

让ReSharper继续"使用系统"; 优化使用时

我想知道是否有一些选项可以让ReSharper不再删除该using System;指令?也许这可以在某处配置?

另外,有没有办法让ReSharper像Visual Studio 2008那样对剩余的指令进行排序(按字母顺序,我认为)?

谢谢.

c# resharper using-directives visual-studio-2008

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

什么是C#"使用"指令?

我在代码示例中看到了这个C#using语句:

using StringFormat=System.Drawing.StringFormat;
Run Code Online (Sandbox Code Playgroud)

那是什么意思?

c# using using-directives

13
推荐指数
3
解决办法
9504
查看次数

在哪里使用命名空间std;

我想知道放在哪里using namespace std;.我看到的代码using namespace std;int main(){} ,但我以后把它#include <iostream>.我应该把它放在哪里,它在我放的地方有什么不同吗?

c++ namespaces using-directives

13
推荐指数
4
解决办法
8404
查看次数

C++ 11`using`关键字:specialize模板参数的模板别名

今天我在使用using关键字时遇到了问题C++11.我现在决定使用另一种方法(在下面的例子中添加为注释).你可以认为X作为一个矩阵,Y作为混入,而目的是访问的tranposed矩阵类型XY.取而代之的typedef荷兰国际集团X<B,A>X<A,B>,我们采取另一种方法是功能更强大,定义一个Sibling别名,它有两个模板参数本身.

template <class A, class B>
struct X
{
  using Left = A;
  using Right = B;
  template <class T1, class T2>
  using Sibling = X<T1, T2>;
  // using Reversed = X<B, A>; // What I really want and use now. :-)
};

template <class A>
struct Y
{
  using Left = typename A::Left;
  using Right = typename A::Right;
  using …
Run Code Online (Sandbox Code Playgroud)

c++ templates using-directives c++11

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

使用表达式找不到模板化的静态constexpr成员函数

对于以下代码

#include <array>

template<unsigned MaxP, typename type>
struct kernel
{
  static constexpr unsigned max_pole(unsigned P)
  { return P>MaxP? MaxP:P; }

  template<unsigned P>
  using array = std::array<type,max_pole(P)>;          // wrong?

  template<unsigned P>
  static void do_something(array<P> const&, array<P>&);
};
Run Code Online (Sandbox Code Playgroud)

gcc 4.7.0(g ++ -c -std = c ++ 11)给出

error: ‘max_pole’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

这是正确的(编译器的行为)吗?请注意,如果我max_pole通过kernel::max_pole在指定的行上替换它来解决它,它编译得很好.

编辑报告给bugzilla,接受为bug c ++/55992,请参阅http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55992.gcc 4.7.x和4.8.0也会出现这种情况.

c++ templates using-directives constexpr c++11

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

friend类声明和using指令

以下示例是否格式正确?

namespace N {
    class A;
}
using namespace N;

class B {
    int i;
    friend class A;
};

namespace N {
    class A {
        B m;
        int get() { return m.i; }
    };
}
Run Code Online (Sandbox Code Playgroud)

这个例子使用Clang 3.5成功编译,但是使用g ++ 4.8.1失败了以下内容:

main.cpp: In member function ‘int N::A::get()’:
main.cpp:7:9: error: ‘int B::i’ is private
     int i;
         ^
main.cpp:14:30: error: within this context
         int get() { return m.i; }
                              ^
Run Code Online (Sandbox Code Playgroud)

C++ 11标准§7.3.1.2p3说,

如果friend声明中的名称既不是限定名也不是模板标识,并且声明是函数或详细类型说明符,则确定实体是否先前已声明的查找不应考虑最内层封闭命名空间之外的任何范围.

例如,class A不是最内层封闭命名空间 …

c++ namespaces using-directives friend name-lookup

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

将'using std :: foo'指令应用于本地构造函数初始化列表(C++)

给定一个自定义类型,下面的片段显示了允许函数自动选择特定于该类型的用户提供的重载的常用方法,或者如果没有,则显示标准库中的函数的泛型实现.

// assume std::foo is a real function template returning an int
namespace a {
  struct b { };    
  int foo(b& ab) { ... }
}

int bar(a::b& ab)
{
  using std::foo;
  return foo(ab);
}
Run Code Online (Sandbox Code Playgroud)

这种方法会自动选择a::foo优先于std::foo(如果存在).

我的问题是,当有问题的调用是构造函数初始化列表的一部分时,是否有可能实现类似的行为?

struct bar2
{
  bar2(a::b& ab);
  int i;
};

bar2::bar2(a::b& ab)
  : i{foo(ab)} // want a::foo if available, else std::foo
{ }
Run Code Online (Sandbox Code Playgroud)

显然,放入using std::foo构造函数体内为时已晚.但是,如果我把它放在构造函数定义之前,我会引入std::foo全局命名空间,这也是不可取的.

在这种情况下,有没有办法让两全其美?

c++ using using-directives c++11

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

什么是命名空间'标准'?

当我尝试编写一个新using子句时,我注意到Intellisense在其列表中有一个名为的命名空间Standard.但是,这似乎没有成员仔细检查.什么是这个命名空间?

c# namespaces using-directives

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

为什么公共重载与某些编译器上的private using指令冲突?

我在一个项目中遇到了以下情况,其中一个基类具有一个函数模板,该模板被派生类模板中的非模板函数隐藏。在类层次结构的更深处,非模板化函数通过using指令将函数显式带入范围。

这是一个简化的示例代码:

class Base
{
public:
  template<typename T> const T& get() const;
};

template<typename T> class Derived : public Base
{
private:
  using Base::get;
public:
  const T& get() const;
};

template<typename T> class MoreDerived : public Derived<T>
{
public:
  using Derived<T>::get; // <-- this causes the problem

  const T& call_get() {
    return get();
  }
};

template class MoreDerived<int>;
Run Code Online (Sandbox Code Playgroud)

Godbolt:https://godbolt.org/z/5MQ0VL

上面的代码在GCC和Clang上失败,出现以下错误:

<source>:15:28: error: 'template<class T> const T& Base::get() const' is inaccessible within this context

   15 | template<typename T> class …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance using-directives

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