相关疑难解决方法(0)

在C++模板实例化中查找依赖名称

当我尝试编译此代码时

// void foobar(int); 

template <class T>
struct Foo {
  void bar(T t) { foobar(t); };
};

void foobar(int);

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

使用g ++ 4.8.2我收到以下错误消息

foo.cc: In instantiation of ‘void Foo<T>::bar(T) [with T = int]’:
foo.cc:10:16:   required from here
foo.cc:5:27: error: ‘foobar’ was not declared in this scope, and no 
             declarations were found by argument-dependent lookup at 
               the point of instantiation [-fpermissive]
   void bar(T t) { foobar(t); };
                           ^
foo.cc:8:6: note: ‘void foobar(int)’ declared here, later in the translation unit …
Run Code Online (Sandbox Code Playgroud)

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

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

从重载模板函数中选择的规则是什么?

鉴于下面的代码,为什么foo(T*)选择该功能?

如果我删除它(foo(T*)代码)代码仍然编译并正常工作,但G ++ v4.4.0(以及可能还有其他编译器)将生成两个foo()函数:一个用于char [4],另一个用于char [7].

#include <iostream>
using namespace std;

template< typename T >
void foo( const T& )
{
    cout << "foo(const T&)" << endl;
}

template< typename T >
void foo( T* )
{
    cout << "foo(T*)" << endl;
}

int main()
{
    foo( "bar" );
    foo( "foobar" );
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates

10
推荐指数
1
解决办法
2100
查看次数