#CPP#Programming#Template#Macro#Code
Code
// 템플릿 함수: 변수명과 값을 출력
template <typename T>
void printVariable(const std::string& name, const T& value) {
std::cout << name << ": " << value << std::endl;
}
// 매크로: 변수명과 값을 전달
#define PRINT_VAR(var) printVariable(#var, var)
Example
example.cpp
#include <iostream>
#include <string>
template <typename T>
void printVariable(const std::string& name, const T& value) {
std::cout << name << ": " << value << std::endl;
}
#define PRINT_VAR(var) printVariable(#var, var)
int main() {
int age = 25;
double height = 175.5;
std::string name = "Alice";
PRINT_VAR(age);
PRINT_VAR(height);
PRINT_VAR(name);
return 0;
}
Compile and Run Command
g++ -o print_example example.cpp && ./print_example
Output
age: 25
height: 175.5
name: Alice