Couldn't deduce template parameter for integer

Hello everybody,

I have the defined a class Seconds with a template<int,int> constructor. Unfortunately, I can't seem to get an instance. Here's the class :

1
2
3
4
5
    class Seconds
    {
        public :
            template <int num, int den> Seconds(long int);
    }


And when I try to instantiate it :

1
2
Seconds *millis = new Seconds<1,1000>(30); //  template argument deduction/substitution failed:
                                           //  couldn't deduce template parameter ‘num’ 


Is it not the right way to call the constructor?

Thanks for your help,
Nicolas
Standard wrote:
14.5.2p5
Because the explicit template argument list follows the function template name, and because conversion member function templates and constructor member function templates are called without using a function name, there is no way to provide an explicit template argument list for these function templates.
All template parameters for constructors should be deducted:

template <typename T> Seconds(T); would work
template <typename T> Seconds( ); would not
Last edited on
I am not sure I understand. The code compiles fine. In my case, the generic is not for the typename, rather for the value of num and den. I took my inspiration from std::ratio. Is it fundamentally wrong to have a constructor like Seconds' ? Why does it work in std::ratio's case and not this one?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// not very useful; might as well make num and den run-time entities
struct seconds 
{
    seconds( double v = 0 ) : value(v) {}

    template < int num, int den > seconds& set( double v )
    {
        value = v * num / den ;
        return *this ;
    }

    template < int num, int den > static seconds init( long v )
    { seconds s ; return s.set<num,den>(v) ; }

    // ...
    double value ;
};

// could be useful; along the lines of std::ratio
template < int num, int den > struct seconds_a_la_ratio
{
    seconds_a_la_ratio( double v = 0 ) : value( v*num/den ) {}

    double value ;
};

int main()
{
    seconds s = seconds::init<1,1000>(30);

    seconds_a_la_ratio<1,1000> s2(30) ;
}
Just made both my mistake and std::ratio much clearer. Thanks so much.
Topic archived. No new replies allowed.