1

The following C++ code uses typeid to print out the runtime class of the parameter:

#include <iostream>

class Foo
{
};

class Bar: public Foo
{
};

template <class O> void printTypeName(O& object)
{
    std::cout << typeid(object).name();
}

int main(void)
{
    Bar x;
    printTypeName(x);
}

Since Foo is not polymorphic, VS C++ doesn't use the object to determine type information and raises

C4100 warning ("unreferenced formal parameter").

Is there any way to get rid of the warning, while preserving the possibility to print out the object type with a simple method call? I would prefer not to have to disable the warning.

quant_dev
  • 6,181
  • 1
  • 34
  • 57

3 Answers3

3

You can use

#pragma warning(disable : 4100)
//.. stuff
#pragma warning(default : 4100)

to turn the warning off and then on again when you're done.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • I know ;-) is there any less brutal way? – quant_dev Sep 16 '11 at 13:26
  • @sharptooth: That question gives absolutely no reason whatsoever for the posted answers. – Puppy Sep 16 '11 at 13:40
  • @quant_dev: You gave absolutely no such requirements in the question. Define "brutal" and edit the question with that definition in and we'll talk. You can't complain about my answer not fulfilling your requirements when those requirements are utterly subjective and were never mentioned in the first place. – Puppy Sep 16 '11 at 13:41
  • @quant_dev: What? I stated some facts. – Puppy Sep 16 '11 at 17:11
  • 1
    Never mind, you won't get it anyway. Bots are notoriously poor when it comes to nuances of human language. – quant_dev Sep 16 '11 at 19:20
1

There's an UNREFERENCED_PARAMETER macro you can use for that.

==== Edited by the OP: one can also use

(void) object;

and avoid using the macro (credits to David Rodriguez for his comment about it).

quant_dev
  • 6,181
  • 1
  • 34
  • 57
Mat
  • 202,337
  • 40
  • 393
  • 406
-1

This works for me without any errors:

template <typename T>
void prn(const T&){
    std::cout << typeid(T).name() << std::endl;
}
j_kubik
  • 6,062
  • 1
  • 23
  • 42