title: In C++, how to use macros to define a vriable under a namespace
body: <p>Here is the case. I want to substitue <code>ComplexDouble</code> type and <code>Double</code> type to <code>ComplexFloat</code> and <code>Float</code> type in one of my files depend on a variables' value.
  An example here:
  Original code:</p>
  <pre><code>Tensor&amp; float_power_out(Tensor&amp; result, const Tensor&amp; base, const Tensor&amp; exp) {
    auto dtype = (at::isComplexType(base.scalar_type()) || at::isComplexType(exp.scalar_type())) ?
                  at::kComplexDouble : at::kDouble;
    TORCH_CHECK(result.scalar_type() == dtype,
                &quot;the output given to float_power has dtype &quot;, result.scalar_type(),
                &quot; but the operation's result requires dtype &quot;, dtype);
  
    return at::pow_out(result, base.to(dtype), exp.to(dtype));
  }
  </code></pre>
  <p>What I expect to do (This is wrong)</p>
  <pre><code>#ifdef __NO_DOUBLE__
  #define at::kComplexDouble at::kComplexFloat
  #define at::kDouble at::kFloat
  #endif
  Tensor&amp; float_power_out(Tensor&amp; result, const Tensor&amp; base, const Tensor&amp; exp) {
    auto dtype = (at::isComplexType(base.scalar_type()) || at::isComplexType(exp.scalar_type())) ?
                  at::kComplexDouble : at::kDouble;
    TORCH_CHECK(result.scalar_type() == dtype,
                &quot;the output given to float_power has dtype &quot;, result.scalar_type(),
                &quot; but the operation's result requires dtype &quot;, dtype);
  
    return at::pow_out(result, base.to(dtype), exp.to(dtype));
  }
  
  #ifdef __NO_DOUBLE__
  #undef at::kComplexDouble
  #undef at::kDouble
  #endif
  </code></pre>
  <p>But using VSCode C++ Intellisense, it informs me that the usage is wrong. It tells me that in my code <code>at::kComplexDouble</code> that <code>the global scope has no &quot;kDouble&quot;</code>. I think its due to that in my <code>#define at::kComplexDouble at::kComplexFloat</code> statement, the <code>::</code> actually get separated out from <code>at</code>. But I don't know how to write the correct syntax, I tried to add some <code>##</code> token concatenations, but not doing it right, I guess. So, how to do this?</p>
  <p>My worst choice is do it like this if I ever need a substition</p>
  <pre><code>Tensor&amp; float_power_out(Tensor&amp; result, const Tensor&amp; base, const Tensor&amp; exp) {
  #ifdef __NO_DOUBLE__
    auto dtype = (at::isComplexType(base.scalar_type()) || at::isComplexType(exp.scalar_type())) ?
                  at::kComplexFloat : at::kFloat;
  #else
    auto dtype = (at::isComplexType(base.scalar_type()) || at::isComplexType(exp.scalar_type())) ?
                  at::kComplexDouble : at::kDouble;
  #endif
    TORCH_CHECK(result.scalar_type() == dtype,
                &quot;the output given to float_power has dtype &quot;, result.scalar_type(),
                &quot; but the operation's result requires dtype &quot;, dtype);
  
    return at::pow_out(result, base.to(dtype), exp.to(dtype));
  }
  </code></pre>
  <p>But there will be 20 occurances of <code>at::kComplexDouble</code> in my file.</p>
  <p>Ok, I have also tried to use <code>using</code> statement to do the substitution, but C++ Intellisense kept telling me my usage is wrong, here is what I tried so far:</p>
  <pre><code>#ifdef __NO_DOUBLE__
  // #define at##::##kComplexDouble at##::##kComplexFloat
  // #define at##::##kDouble at##::##kFloat
  // #define toComplexDouble toComplexFloat
  // #define toDouble toFloat
  using at::kComplexFloat = at::kComplexDouble;
  using at::kFloat = at::kDouble;
  #endif
  Tensor&amp; float_power_out(Tensor&amp; result, const Tensor&amp; base, const Tensor&amp; exp) {
    auto dtype = (at::isComplexType(base.scalar_type()) || at::isComplexType(exp.scalar_type())) ?
                  at::kComplexDouble : at::kDouble;
    TORCH_CHECK(result.scalar_type() == dtype,
                &quot;the output given to float_power has dtype &quot;, result.scalar_type(),
                &quot; but the operation's result requires dtype &quot;, dtype);
  
    return at::pow_out(result, base.to(dtype), exp.to(dtype));
  }
  </code></pre>
  <p>It kept telling me <code>qualified name is not allowed</code>, and <code>c10::kComplexDouble is not a type name</code> error.</p>
  <p>Besides, I don't know how to limit the range of <code>using</code> satements, meaning I don't want  <code>at::kFloat</code> to mean <code>at::kDouble</code> anymore after some blocks of code here.</p>
  <p>Edited:
  Thanks for the answer provided by @molbdnilo. Besides, I just find that there are also some functions I need to substitute to <code>float</code> versions. For example, function calling <code>toComplexDouble()</code> and <code>toDouble()</code>, I want it to actually call <code>toComplexFloat()</code> and <code>toFloat()</code> if I defined my <code>__NO_DOUBLE__</code> macro. So, would it work if I write it like this:</p>
  <pre><code>#ifdef __NO_DOUBLE__
  #define toComplexDouble toComplexFloat
  #define toDouble toFloat
  
  
  ...
  #ifdef __NO_DOUBLE__
  #undef toComplexDouble
  #undef toDouble
  </code></pre>
  <p>Is the <code>#undef</code> needed here?</p>
  <p>Edit:
  Now I just figured out how not to include the namespace operators <code>::</code> in my macro define statements, here is my solution, I think I need minimal code changes comparing to using <code>const auto</code> solution. Is this use case of macros bad practice?</p>
  <pre><code>#ifdef __NO_DOUBLE__
  #define kComplexDouble kComplexFloat
  #define kDouble kFloat
  #define toComplexDouble toComplexFloat
  #define toDouble toFloat
  #endif
  Tensor&amp; float_power_out(Tensor&amp; result, const Tensor&amp; base, const Tensor&amp; exp) {
    auto dtype = (at::isComplexType(base.scalar_type()) || at::isComplexType(exp.scalar_type())) ?
                  at::kComplexDouble : at::kDouble;
    TORCH_CHECK(result.scalar_type() == dtype,
                &quot;the output given to float_power has dtype &quot;, result.scalar_type(),
                &quot; but the operation's result requires dtype &quot;, dtype);
  
    return at::pow_out(result, base.to(dtype), exp.to(dtype));
  }
  
  ...
  #ifdef __NO_DOUBLE__
  #undef kComplexDouble
  #undef kDouble
  #undef toComplexDouble
  #undef toDouble
  #endif
  </code></pre>