开发者

Is there a way to cout a #defined term?

#define BLAH word

cout << BLAH;
开发者_JS百科

Is there any way to do this?


Try

#define STRINGIFY(x) #x
#define STRINGIFYMACRO(y) STRINGIFY(y)

#define BLAH word
cout << STRINGIFYMACRO(BLAH);

The extra level of indirection causes the value of the macro to be stringified instead of the name of the macro.

Tested on ideone.


  1. 'cout' is not something you do. It is a global variable, which is an instance of the std::ostream type. You could say e.g. "output to cout".

  2. #define does textual substitution. It is basically the same as if you used search-and-replace in your text editor to replace BLAH with word. Thus, the line cout << BLAH; turns into cout << word;. If it's not working, it's because cout << word; isn't a valid statement in that scope. The preprocessor does not care about any of the surrounding text. It has basically zero understanding of the code (it knows how to tokenize the code, i.e. break apart operators and other punctuation if you don't put in any space, but that's about it.)


If you want to print the word "word", you can do:

#define STRINGIFY(x) #x

cout << STRINGIFY(word);


I suspect you want something like this:

#include <typeinfo>

template <typename T>
void print_type()
{
    std::cout << typeid(T).name() << std::endl;
}

int main()
{
    print_type<float>();
    print_type<int>();
}

Note that value of typeid(T).name() is implementation defined, and may be nothing at all. There is no way to print out a type in a guaranteed way without writing a function yourself, for each type.

You can make an overload that deduces the type of the expression too:

#include <typeinfo>

template <typename T>
void print_type(const T&)
{
    std::cout << typeid(T).name() << std::endl;
}

int main()
{
    print_type(5.0f);
    print_type(5.0);
}

(Note that this evaluates the expression, which is unnecessary, but I doubt that is a concern.)


I'm testing a program for different representations of number, i.e. float and double. I want it to print out: "Testing for float" when the defined term is float.

No need to abuse macros as a primitive typedef:

template<class T>
void do_test(char const *name) {
  std::cout << "Testing " << name << "...\n";
  T var = foo();
  bar(var);
}

int main() {
  do_test<float>("single precision");
  do_test<double>("double precision");
  return 0;
}

Notice this lets you give different names (hopefully more meaningful) to each test, rather than just stringizing the parameters to the test, but "float" and "double" could be the names if you like.

If you really, really wanted to stringize the parameters – or if you're just curious about macros:

#define DO_TEST(T) do_test<T>(#T)
int main() {
  DO_TEST(float);
  DO_TEST(double);
  return 0;
}


OK, let's try this again, based on your comment:

I'm testing a program for different representations of number, i.e. float and double. I want it to print out: "Testing for float" when the defined term is float.

It sounds like this is what you really mean:

I have code like the following:

cout << "As a float:" << float(value) << endl;
cout << "As a double:" << double(value) << endl;

and I want to make a macro for each of these lines so that the text matches the type-cast.

In that case, you want something like this:

#define DEBUG(t, x) cout << "As a " #t ":" << t(x) << endl

DEBUG(float, value);
DEBUG(double, value);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜