template member function can be called from outside class but not within

E

er

hi!

basically:
A::f<index>();//OK
A::g(index);//Fails//internally calls f<index>
C::f(index);//Fails.

why do i care? i'd like to make A::f private.

any help appreciated. thanks!

class A{
public:
A(unsigned int init_):init(init_){};
//unsigned int g(unsigned int index){return f<index>();};
//-error: 'index' cannot appear in a constant-
expression
//-error: no matching function for call to 'A::f()'
template<unsigned int i> unsigned int f(){
return init+i;
};
private:
unsigned int init;
};
class C{
public:
C(unsigned int init_):init(init_){};
template<unsigned int i> unsigned int f(unsigned int i){
return init+i;
};//error: no matching function for call to 'C::f(int)'
private:
unsigned int init;
};
 
V

Victor Bazarov

er said:
basically:
A::f<index>();//OK
A::g(index);//Fails//internally calls f<index>
C::f(index);//Fails.

why do i care? i'd like to make A::f private.

any help appreciated. thanks!

class A{
public:
A(unsigned int init_):init(init_){};
//unsigned int g(unsigned int index){return f<index>();};

In the 'g's body "index" has only its value during the run-time.
In order to call a template with "index" as its argument you
need 'index' to be a compile-time constant expression.
//-error: 'index' cannot appear in a constant-
expression
//-error: no matching function for call to 'A::f()'
template<unsigned int i> unsigned int f(){
return init+i;
};
private:
unsigned int init;
};
class C{
public:
C(unsigned int init_):init(init_){};
template<unsigned int i> unsigned int f(unsigned int i){
return init+i;
};//error: no matching function for call to 'C::f(int)'

You can't have 'i' as the function template argument and 'i' as
the function argument. Pick one.
private:
unsigned int init;
};

V
 
E

er

In the 'g's body "index" has only its value during the run-time.
In order to call a template with "index" as its argument you
need 'index' to be a compile-time constant expression.


You can't have 'i' as the function template argument and 'i' as
the function argument. Pick one.


V

ok, thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top