[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 951
  • Last Modified:

Failed to specialize function template

This is a really odd error, it's happening in this context:

template<class _Iter>
_Vector(_Iter first, _Iter last)
{
  std::distance(first,last);
}

I've removed some code from the above, but the error still happens with just that left.

As best I can tell from the source, std::distance isn't specialized, although it uses some types from iterator_traits which is.

But the oddest part is I don't even invoke that constructor anywhere, so how does it know it can't specialize based on _Iter? it doesn't know what type it is yet!

Where do I start with solving this problem?

thanks,
-Sandra

0
Sandra-24
Asked:
Sandra-24
  • 7
  • 6
  • 4
  • +3
4 Solutions
 
jkrCommented:
Hmm, just tried to compile

#include <iterator>

template<class _Iter>
_Vector(_Iter first, _Iter last)
{
 std::distance(first,last);
}

Works just fine.
0
 
lbertaccoCommented:
GCC 3.3 complains about:
test.cpp:5: error: ISO C++ forbids declaration of `_Vector' with no type

which is true of course. What is _Vector supposed to be? a function? Then add a "void" before _Vector, or return the distance...
0
 
AxterCommented:
I would try removing all the prefix underscores from the code.
Try using postfix underscores instead:

template<class Iter_>
Vector_(Iter_ first, Iter_ last)
{
 std::distance(first,last);
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AxterCommented:
More then likely the problem is related to the missing return type as lbertacco has stated.
However, you should know that using prefix underscores in your code, makes your code none-portable.
According to the C++ standards, names that begain with underscore are reserve for implementation.
0
 
Sandra-24Author Commented:
_Vector() is a constructor in a class by that name.

I generally use the prefix underscore to denote implementation stuff. Make it easier to ignore them in the member lists and such. For example, _Vector is only provided to be inherited from and to keep it sperate from Vector and std::vector. I could use another naming scheme, but this one is convienient, and since this is personal code, portability is not a major issue.

I think I have simultaneusly arrived at the solution to two problems though. I did wonder why there was a specialization of the base insert function in std::vector for _Int_iterator_tag iterators that basically does _Insert_n(where,(size_type)first,(_Ty)last);

I couldn't figure out where that could be used, since how often would an iterator be able to be cast to both size_type and the value type of the container. The reason for this is constructing a vector<int> with (10,5) actually calls the (Iter_ first, Iter_ last) constructor. So technically first and last in this case is count and val. I'm going to implement that and it should fix the problem. I'll keep you posted.
0
 
Sandra-24Author Commented:
An unfortuanate thing about the STL is they keep _Int_iterator_tag as implementation defined. It's not standard. So I had to create my own iterator_traits, and my own specialized distance() function to deal with it. That solved the problem though.
0
 
Sandra-24Author Commented:
Actually as something to do with the points of this question,do you see anything wrong with the specialized _Distance function below? It's not compiling. error C2768: 'Collections::_Distance' : illegal use of explicit template arguments


      //again since std::distance can't deal with Int_iterator_tag iterators we must define our own that does
      template<class _Iter>
            typename iterator_traits<_Iter>::distance_type distance(const _Iter& first, const _Iter& last)
      {
            return _Distance(first,last,get_iterator_category(first));
      }

      template<class _Iter, class _Tag>
            typename iterator_traits<_Iter>::distance_type _Distance(const _Iter& first, const _Iter& last, _Tag)
      {
            return std::distance(first,last); //defer to std::distance
      }

      template<class _Iter>
            typename iterator_traits<_Iter>::distance_type _Distance<_Iter,Int_iterator_tag>(const _Iter& first, const _Iter& last, Int_iterator_tag)
      {
            return (typename iterator_traits<_Iter>::distance_type)first;
      }

cheers,
-Sandra
0
 
jkrCommented:
Without VC7 here, just a shot in the dark - you are using a template argument in a specialization, shouldn't

    template<class _Iter>
         typename iterator_traits<_Iter>::distance_type _Distance<_Iter,Int_iterator_tag>(const _Iter& first, const _Iter& last, Int_iterator_tag)

read

    template<class _Iter>
         typename iterator_traits<_Iter>::distance_type _Distance<Int_iterator_tag>(const _Iter& first, const _Iter& last, Int_iterator_tag)

?

0
 
AxterCommented:
>>_Vector() is a constructor in a class by that name.

Is this class declaration inside of the class declaration?
template<class _Iter>
_Vector(_Iter first, _Iter last)
{
 std::distance(first,last);
}

Can you post the entire class declaration so we can get a full picture.

If this code is outside of the class declaration, then the compiler is probly thinking you're trying to declare a class instead of declaring the constructor.
0
 
stefan73Commented:
Hi Sandra-24,
> As best I can tell from the source, std::distance isn't specialized

Template functions are implicitly specialized by their parameters.

Cheers!

Stefan
0
 
Sandra-24Author Commented:
Nope jkr, same error.

I'm going to take a break and come back to it later.

Axter, I fixed the problem regarding the error with the constructor, read my post above, thanks though.
0
 
Sandra-24Author Commented:
Actually it seems you must omit the template argument after the function name.

   template<class _Iter>
         typename iterator_traits<_Iter>::distance_type _Distance(const _Iter& first, const _Iter& last, Int_iterator_tag)
0
 
Sandra-24Author Commented:
The reason for the original error is that distance_type is not part of the specialized iterator_traits struct.

I'm splitting the points of this question evenly among everyone who replied.
0
 
AxterCommented:
Thanks Dan,
But what is Sage Level, and what does it do for the expert?
This is the first I've heard of it.
0
 
jkrCommented:
>>But what is Sage Level

It means that you earned 500k pts in a single TA

>>and what does it do for the expert?

Err, um, nothing - at least, nothing that I would know about :o)
0
 
AxterCommented:
Never mind Dan.
I just logged on using the new EE site, and I see the info box with the level description.
Believe it or not, I still log on to the site using "oldlook.experts-exchange.com" link, which of course makes no mention of the levels.

I'm still curious if there's any benifit to the expert to have the higher levels.
0
 
AxterCommented:
>>Err, um, nothing - at least, nothing that I would know about :o)

OK Thanks.  Just curious.

I guess it's just another benchmark.
0
 
jkrCommented:
>>Believe it or not, I still log on to the site using "oldlook.experts-exchange.com"

Believe it or not, you are not alone :o)
0
 
DanRollinsCommented:
It's A milestone... and an important one.  There are very few Sage-level experts.  Consider yourself among the site elite :)
-- Dan
0
 
DanRollinsCommented:
Axter, if you switch to "Expert Mode" the newlook version is perfectly usable and it has a few features that make the switch worthwhile.
0
 
AxterCommented:
>>Axter, if you switch to "Expert Mode" the newlook version is perfectly usable and it has a few features that make the switch worthwhile.

You're right!!!
Why haven't they advertise this???
I've even posted multiple complaints about the new look, and you're the first person who said anything about this.

Thanks!
This has made my day.  (even more so then the Sage thing).
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 7
  • 6
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now