Odd Visual Studio Behaviour

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
 
R

Ron Natalie

int func1(int verbosity){
return func1_T<verbosity>;
I'm not sure why this compiles. Are you missing () here?
result = myclass.func1(rand()%4); // amazingly this compiles !!!

Nothing wrong with this. You're just passing an int to a member
function that is expecting one.
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 ?
I think it's a bug in visual studio, but in a different place
than you are expecting.
 
M

m.tyka

I just realised i asked what appears to be a compiler-dependent
question but the fact i compiled this on MSVC is really secondary -
what i'm really after is what a general, compliant compiler *should* do
in this situation rather than what MSVC happens to do.

thanks. Mike
 
M

m.tyka

I just realised i asked what appears to be a compiler-dependent
question but the fact i compiled this on MSVC is really secondary -
what i'm really after is what a general, compliant compiler *should* do
in this situation rather than what MSVC happens to do.

thanks. Mike
 
S

Sumit Rajan

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>;


It's indeed surprising that this compiles. My understanding is that it
should not. It does not to compile on Comeau or g++.

Regards,
Sumit.
 
F

F.J.K.

return func1_T<verbosity>;

You are returning a function pointer, but you probably know that by now
and are pissed of your compiler didn't warn/error you ;-)

I got your testcase reduced to the following:
#include <iostream>
struct S{
int foo() { return bar<int>; }
template<int> void bar();
};
int main(){
S s;
std::cout << s.foo() << "\n";
}

which outputs:
0

on my machine. Notice the strange " int foo() { return bar<int>; }"
This only "works" within a class declaration. Probably this has to do
with some weird forward declaration rules in classes, but I'm as
baffled as you are.

g++-4.1.1 errors out on this code with:

test.cpp: In member function 'int S::foo()':
test.cpp:3: error: cannot resolve overloaded function 'bar' based on
conversion
to type 'int'

My guess would be, this is a genuine bug in VS2005 where the compiler
produces some dummy bar<int> member function, function pointer
initialised to zero, that shouldn't be visible to the source.

It would be interesting to hear what people will tell, who have read or
written the standard ;-)

Greets, Felix
 

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,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top