Saturday, July 14, 2018

c++ - Error when manually instantiating a template



Recently at my company we ran into an error that I am having trouble understanding why it's actually an error. To us it seems as though this should compile just fine and allow us to explicitly instantiate a template of type bar::foo.



mainy.cxx



int foo(int);
namespace bar {
template T foo(T a, T){return a;}
}


namespace bar {
using ::foo;
}

template int bar::foo(int, int);

int main(){
return 0;
}



g++ error



[csteifel@host:~/test]1047 $ g++ mainy.cxx
mainy.cxx:10: error: 'int bar::foo(int, int)' should have been declared inside 'bar'
mainy.cxx:10: error: 'int bar::foo(int, int)' is not declared in '::'


We have confirmed that this is an error in gcc 4.8, 4.4 and clang 3.7 however it appears to work with Visual Studio 2015.




We ran into this problem when we tried to instantiate std::remove but had included before and has in it



namespace std {
using ::remove;
}


Any ideas on whats going on here?


Answer




Looks like this is related to an ancient bug in gcc, where you cannot explicitly instantiate a template by using ns::func, the only way was to do it using namespace ns { ... func; }. This was only recently fixed and with newer gcc your code will compile.



And by the way, contrary to what you are saying, your code compiles with clang 3.7.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...