Solved

Failed to specialize function template

Posted on 2004-03-20
22
932 Views
Last Modified: 2012-06-27
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
Comment
Question by:Sandra-24
  • 7
  • 6
  • 4
  • +3
22 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
Comment Utility
Hmm, just tried to compile

#include <iterator>

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

Works just fine.
0
 
LVL 11

Assisted Solution

by:lbertacco
lbertacco earned 50 total points
Comment Utility
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
 
LVL 30

Assisted Solution

by:Axter
Axter earned 200 total points
Comment Utility
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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
 
LVL 3

Author Comment

by:Sandra-24
Comment Utility
_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
 
LVL 3

Author Comment

by:Sandra-24
Comment Utility
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
 
LVL 3

Author Comment

by:Sandra-24
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>_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
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 50 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Author Comment

by:Sandra-24
Comment Utility
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
 
LVL 3

Author Comment

by:Sandra-24
Comment Utility
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
 
LVL 3

Author Comment

by:Sandra-24
Comment Utility
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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>Err, um, nothing - at least, nothing that I would know about :o)

OK Thanks.  Just curious.

I guess it's just another benchmark.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
It's A milestone... and an important one.  There are very few Sage-level experts.  Consider yourself among the site elite :)
-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now