Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #5370

Template argument determination

From Ruben Safir <ruben@mrbrklyn.com>
Newsgroups comp.lang.c++
Subject Template argument determination
Date 2011-05-23 20:31 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <ireg6t$5nh$1@reader1.panix.com> (permalink)

Show all headers | View raw


I'm having trouble understanding why my program is making a change in the 
argument type of my template class function

   template < class T >
      void List<T>::find_value(T val)
      {
	 cursor() = front();
	 while(cursor() != endd()){
	    if (*(cursor()->value()) == val)
	       return;
	    else{
	       cursor(cursor()->next());
	    }
	 }
	 if(*(endd()->value()) == val)
	    return;
	 else{
	    cursor() = 0; //park the cursor when nothing is found
	 }
      }


It's part of a List template class

I call it from a templated fucntion in a different namespace as follows:


tally = new chainlist::List<stats::Distribution<int> >;
tallies = new chainlist::List< chainlist::List< stats::Distribution<int> > *>;

std::cout << "The standard mean for picking 7 is ==> " << stats::mean_list(tallies, 7 ) << std::endl;;

   template<typename T>
      float mean_list(chainlist::List< chainlist::List<stats::Distribution<T> >*  > *  tallies, T search_val){
	 if(tallies->endd() == 0){
	    std::cout << "Empty List" << std::endl;
	    return 0.0;
	 }
	 int sum = 0;
	 chainlist::List<stats::Distribution<T> > * dump;
	 tallies->cursor() = tallies->front();
	 while(tallies->cursor() != tallies->endd() ){
		 //tallies->cursor()->value()->find_value(search_val);
		 dump = *(tallies->cursor()->value());
		 //dump->cursor() = dump->endd();
		 //std::cout << "Testing\n" << *(dump->cursor()->value()) << std::endl;
		 dump->find_value(search_val);
		 if(dump->cursor() != NULL)
		    sum +=  dump->cursor()->value()->population();
		 tallies->cursor(tallies->cursor()->next());
	 }
	 dump = *(tallies->cursor()->value());
	 dump->find_value(search_val);
	 if(dump->cursor() != 0)
	    sum +=  dump->cursor()->value()->population();
	 float tot = (float) sum/(float)(tallies->size());
	 std::cout << "Mean " << tot << " sum " << sum << " size " << tallies->size() << std::endl;
	 return tot;

      }

Now T is an integer for mean_list, and the first argument is of type 

chainlist::List< chainlist::List<stats::Distribution<int> >*  > * when called and the second argument is of type int

I then pass int search_val to method chainlist::List<T>::find_value(T) where T = stats::Distribution<T> (where T is an int)
on this line:

dump->find_value(search_val);

and that's where the weirdness happens.  I have a type mismatch since I'm sending by value an integer instead of a stats::Distribution<T>

Instead the program calls the stat::Distribution constructor using 7, the value of search_val, as an argument redrawing search_val as type
to a Distribution<int> 

:in gdb


 312       void List<T>::find_value(T val)
 313       {
 314          cursor() = front();
 315          while(cursor() != endd()){
 316             if (*(cursor()->value()) == val)
 317                return;
 318             else{
 319                cursor(cursor()->next());
 320             }
 321          }
 322          if(*(endd()->value()) == val)
 323             return;
 324          else{
 325             cursor() = 0; //park the cursor when nothing is found
 326          }
 327       }

chainlist::List<stats::Distribution<int> >::find_value (this=0x804f008, val=...) at linklist.h:314
(gdb) ptype val
type = class stats::Distribution<int> {
  public:
    chainlist::List<stats::Distribution<int> > *tally;
  private:
    int freq;
    int occurances;

  public:
    void Distribution(int, int);
    void Distribution(void);
    int description(void) const;
    int population(void) const;
    void increase_occ(void);
    void descrease_occ(void);
    int operator()(void);
    bool operator==(stats::Distribution<int> &);
    bool operator<(stats::Distribution<int> &);
    float stddev(chainlist::List<stats::Distribution<int> > *);
}


What rule for Template argument matching allows for this kind of magic?

Back to comp.lang.c++ | Previous | NextNext in thread | Find similar


Thread

Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-23 20:31 +0000
  Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-24 02:50 +0000
    Re: Template argument determination Michael Doubez <michael.doubez@free.fr> - 2011-05-24 03:48 -0700
    Re: Template argument determination "Balog Pal" <pasa@lib.hu> - 2011-05-24 13:06 +0200
      Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-24 14:44 +0000
        Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-24 15:04 +0000
          Re: Template argument determination "Balog Pal" <pasa@lib.hu> - 2011-05-24 17:33 +0200
          Re: Template argument determination Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-24 11:43 -0400
  Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-24 12:32 -0500
    Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-24 18:08 +0000
      Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-24 14:32 -0500
        Re: Template argument determination Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-24 15:48 -0400
          Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-24 17:11 -0500
            Re: Template argument determination Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-24 19:04 -0400
              Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-25 12:37 -0500
                Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-26 16:12 +0000
                Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-26 13:25 -0500
            Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-25 02:47 +0000
              Re: Template argument determination "Balog Pal" <pasa@lib.hu> - 2011-05-25 10:06 +0200
              Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-25 12:40 -0500

csiph-web