M
m.tyka
To my surprise the following code compiled on Visual Studio 2005:
#include <stdlib.h>
#include <iostream>
class MyClass{
public:
int func1(int verbosity){
return func1_T<verbosity>;
};
int func1_veryverbose(){
return func1_T<2>();
};
int func1_verbose(){
return func1_T<1>();
};
int func1_fast(){
return func1_T<0>();
};
private:
template <int verbose>
int func1_T(){
std::cout << "Mode: ";
if(verbose==2){
std::cout << "Verbose Mode" << std::endl;
}
if(verbose==1){
std::cout << "Normal Mode" << std::endl;
}
if(verbose==0){
std::cout << "Fast Mode" << std::endl;
}
return 0;
}
};
int main(){
int result;
MyClass myclass;
result = myclass.func1_fast();
result = myclass.func1_verbose();
result = myclass.func1_veryverbose();
result = myclass.func1(rand()%4); // amazingly this compiles !!!
return 0;
}
------
Program output:
Mode: Fast Mode
Mode: Normal Mode
Mode: Verbose Mode
further the program just calls the the first 3 calls but
understandably nothing happens on the 4th,
the compiler can't (shouldn't?) make a template for every possible
template parameter.
But why does the compiler not warn about this ??
Can anyone shed any light on this ?
Cheers,
Mike
#include <stdlib.h>
#include <iostream>
class MyClass{
public:
int func1(int verbosity){
return func1_T<verbosity>;
};
int func1_veryverbose(){
return func1_T<2>();
};
int func1_verbose(){
return func1_T<1>();
};
int func1_fast(){
return func1_T<0>();
};
private:
template <int verbose>
int func1_T(){
std::cout << "Mode: ";
if(verbose==2){
std::cout << "Verbose Mode" << std::endl;
}
if(verbose==1){
std::cout << "Normal Mode" << std::endl;
}
if(verbose==0){
std::cout << "Fast Mode" << std::endl;
}
return 0;
}
};
int main(){
int result;
MyClass myclass;
result = myclass.func1_fast();
result = myclass.func1_verbose();
result = myclass.func1_veryverbose();
result = myclass.func1(rand()%4); // amazingly this compiles !!!
return 0;
}
------
Program output:
Mode: Fast Mode
Mode: Normal Mode
Mode: Verbose Mode
further the program just calls the the first 3 calls but
understandably nothing happens on the 4th,
the compiler can't (shouldn't?) make a template for every possible
template parameter.
But why does the compiler not warn about this ??
Can anyone shed any light on this ?
Cheers,
Mike