Generating a dictionnary for template function

Hi Rooters

How can I create a dictionnary for template functions? Say I have this template function:

template <class T>
void printVector(const std::vector<T>& a)
{
    for (size_t i = 0; i < a.size(); ++i) {
        std::cout  << a[i] << " ";
    }
    std::cout  << std::endl;
 
   return ;
}

What should I do in my LinDef.h file if I want to generate dictionnaries for, say “int” and “float”?
I tried

#ifdef __CINT__

#pragma link C++ function printVector(const std::vector<float>& a);
#pragma link C++ function printVector(const std::vector<int>& a);

#endif

but it doesn’t work. I also tried “#pragma link C++ function printVector(const std::vector& a);” but it doesn’t work either.

I am using ROOT 5.18.00 on a Linux box, gcc 3.3.5

Thanks

Regards

Hi,

You should not name the parameter in the linkdef lines:[code]
#ifdef CINT

#pragma link C++ function printVector(const std::vector&);
#pragma link C++ function printVector(const std::vector&);

#endif [/code]should work.

Cheers,
Philippe.

Hi Philippe

Thanks for pointing out one of my mistake. It still doesn’t work, so there is still something wrong with what I am doing:

file printVecor.hxx

#ifndef __PRINTVECTOR_HXX__
#define __PRINTVECTOR_HXX__

#include <iostream>

template <class T>
void printVector(const std::vector<T>& a)
{
  
    for (size_t i = 0; i < a.size(); ++i) {
        std::cout  << a[i] << " ";
    }
    std::cout  << std::endl;
    
};

#endif

file LinkDef.h:

#ifdef __CINT__

#pragma link C++ function printVector(const std::vector<int>& );

#endif

And the command to generate the dictionnary:

rootcint -f dict.cxx -c printVector.hxx LinkDef.h

But I have the following message:

Do I need to instantiate the template somehow before generating the dictionnary?

Thank for your help

Hi,

This is due to one of the deficiencies of CINT that we hope to fix later this year in the way it deal with the std namespace. To work around this issue, declare the template as: #ifdef __CINT__ template <class T> void printVector(const vector<T>& a) #else template <class T> void printVector(const std::vector<T>& a) #endif

Cheers,
Philippe.

Hello Philippe

Thanks for your answer. I will use your workaround or use a “use std::vector” in my header files for the time been.

Cheers

[quote=“pcanal”]Hi,

This is due to one of the deficiencies of CINT that we hope to fix later this year in the way it deal with the std namespace. To work around this issue, declare the template as: #ifdef __CINT__ template <class T> void printVector(const vector<T>& a) #else template <class T> void printVector(const std::vector<T>& a) #endif
[/quote]

This is no longer a root issue, but since templates definitions are usually '#include’d, they should follow header file style rules. ‘using namespace std;’ is usually strongly discouraged (and even if it’s not, you are free to use the std:: identifier.

In other words

template <class T>
void printVector(const std::vector<T>& a) 

should be sufficient in all cases and preferable in most.

Cheers,
Charles