Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Failed to specialize function template

Posted on 2004-03-20
22
Medium Priority
?
948 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 4
  • +3
22 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 800 total points
ID: 10641449
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 200 total points
ID: 10641526
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 800 total points
ID: 10641531
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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Expert Comment

by:Axter
ID: 10641537
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
ID: 10641654
_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
ID: 10642006
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
ID: 10642012
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
ID: 10642069
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
ID: 10642100
>>_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 200 total points
ID: 10642278
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
 
LVL 3

Author Comment

by:Sandra-24
ID: 10642487
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
ID: 10642897
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
ID: 10643237
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
ID: 10644216
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
ID: 10644227
>>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
ID: 10644231
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
ID: 10644238
>>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
ID: 10644242
>>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
ID: 10645219
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
ID: 10645356
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
ID: 10645884
>>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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

636 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