【问题标题】:How to create a macro for a templated function in c++如何在 C++ 中为模板化函数创建宏
【发布时间】:2016-10-04 10:10:05
【问题描述】:

我想为模板函数创建一个宏

#define CHECK_MIN(T)(value, target) checkmin(T value, T target) \
checkmin<T>(T val, T target, __FUNCTION__, __LINE__)

template<typename T> checkmin(T val, T target, const char* functionName, long lineNumber)
{
    // Check if the val is less than target
    // Construct a std::string using the function name and line name
    // throw std:: exception passing the string constructed above.
}

我无法获得实现此目的的语法。有任何想法吗?

【问题讨论】:

    标签: c++ templates visual-c++ macros


    【解决方案1】:

    您可以通过以下方式进行:

    #include <iostream>
    using namespace std;
    
    template<typename T>
     void  checkmin(T val, T target, const char* functionName, long lineNumber)
     {
       // Check if the val is less than target
       // Construct a std::string using the function name and line name
       // throw std:: exception passing the string constructed above.
     }
    
    #define CHECK_MIN(value, target)  \
     checkmin(value,target, __FUNCTION__, __LINE__);
    
    int main() {
        /*Not using new in object definitions so I don't have to delete them afterwards since pointers don't stay in memory*/
        CHECK_MIN(5,6);
    
         return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-29
      • 1970-01-01
      • 2010-11-01
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 2021-08-14
      相关资源
      最近更新 更多