Share via


依存型の名前解決

特定の修飾名が型を識別するようにコンパイラに指示するためにテンプレート定義で修飾された名前に 種類名 を使用します。詳細については、「型名」を参照してください。

// template_name_resolution1.cpp
#include <stdio.h>
template <class T> class X
{
public:
   void f(typename T::myType* mt) {}
};

class Yarg
{
public:
   struct myType { };
};

int main()
{
   X<Yarg> x;
   x.f(new Yarg::myType());
   printf("Name resolved by using typename keyword.");
}

dx2zs2ee.collapse_all(ja-jp,VS.110).gif出力

Name resolved by using typename keyword.

依存名の名前検索は両方の名前を次の例のテンプレート定義のコンテキスト検査して、このコンテキストは myFunction(char) (テンプレートをインスタンス化コンテキスト検索します。次の例では、テンプレートでは、メイン インスタンス化されています; したがって、MyNamespace::myFunction はインスタンス化の点から表示され、より適合する演算子として採用されます。MyNamespace::myFunction の名前を変更した場合、myFunction(char) が代わりに呼び出されます。

すべての名前は依存名のように解決されます。いずれにしても、可能な競合がある場合は、完全修飾名を使用することをお勧めします。

//template_name_resolution2.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

void myFunction(char)
{
   cout << "Char myFunction" << endl;
}

template <class T> class Class1
{
public:
   Class1(T i)
   {
      // If replaced with myFunction(1), myFunction(char)
      // will be called
      myFunction(i);
}
};

namespace MyNamespace
{
   void myFunction(int)
   {
      cout << "Int MyNamespace::myFunction" << endl;
   }
};

using namespace MyNamespace;

int main()
{
   Class1<int>* c1 = new Class1<int>(100);
}

dx2zs2ee.collapse_all(ja-jp,VS.110).gif出力

Int MyNamespace::myFunction

dx2zs2ee.collapse_all(ja-jp,VS.110).gifテンプレートの区別

Visual Studio 2012 の Visual C++ は 「テンプレート」キーワードの区別の C++98/03/11 標準規則が適用されます。次の例では、Visual C++ 2010 が不正な線やに準拠した行の両方を受け取ります。Visual Studio 2012 の Visual C++ はに準拠した行のみを受け入れます。

#include <iostream>
#include <ostream>
#include <typeinfo>
using namespace std;

template <typename T> struct Allocator {
    template <typename U> struct Rebind {
        typedef Allocator<U> Other;
    };
};

template <typename X, typename AY> struct Container {
    #if defined(NONCONFORMANT)
        typedef typename AY::Rebind<X>::Other AX; // nonconformant
    #elif defined(CONFORMANT)
        typedef typename AY::template Rebind<X>::Other AX; // conformant
    #else
        #error Define NONCONFORMANT or CONFORMANT.
    #endif
};

int main() {
    cout << typeid(Container<int, Allocator<float>>::AX).name() << endl;
}

区別の規則の照合は AY::Rebind テンプレートは、想定するため、コンパイラが、として次の 「」を<未満でない場合、既定では、C++ を解釈するため、必須です。右山かっことして正しく 「」<を分析できるように Rebind がテンプレートであることを確認する必要があります。

参照

関連項目

テンプレートおよび名前解決