Link to home
Start Free TrialLog in
Avatar of Sandra-24
Sandra-24

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of Sandra-24
Sandra-24

ASKER

_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.
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.
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
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)

?

>>_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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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)
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.
Thanks Dan,
But what is Sage Level, and what does it do for the expert?
This is the first I've heard of it.
>>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)
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.
>>Err, um, nothing - at least, nothing that I would know about :o)

OK Thanks.  Just curious.

I guess it's just another benchmark.
>>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)
It's A milestone... and an important one.  There are very few Sage-level experts.  Consider yourself among the site elite :)
-- Dan
Axter, if you switch to "Expert Mode" the newlook version is perfectly usable and it has a few features that make the switch worthwhile.
>>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).