【发布时间】:2020-11-28 15:41:53
【问题描述】:
我做了一个console.log 函数来模仿javascript 的console.log 函数。我遇到的问题是在所有参数之后换行,因为我无法检测何时是单个参数调用,或者是由于函数重载而调用的最后一个参数。
我认为的解决方案是从头文件中为函数添加一个新参数。
所以当我写在 main.cpp 中时:
console.log("hello");
console.log("bye");
我希望您在从头文件获取任何处理之前帮助我添加一个新参数:
console.log("hello", "");
console.log("bye", "");
完整的代码在这里。 https://github.com/StringManolo/cppjs/blob/main/console/log.h#L17
这是您要求的最小可重现示例: main.cpp
#include "./log.h"
int main() {
/* Each console.log call should add a line break */
console.log("Line1");
console.log("Line2");
console.log("Line3");
/* Like Here */
console.log("Line1", "");
console.log("Line2", "");
console.log("Line3", "");
/* Then i can do: */
console.log("Welcome.");
console.log("7 * 8 = ", 7 * 8);
console.log("Bye!");
std::vector<std::string> example = {"a", "b", "c", "d", "e"};
console.log(example);
std::any numberOrString = 1337;
console.log(numberOrString);
numberOrString = (std::string) "String";
console.log(numberOrString);
/* stdout :
$ ./test
Line1Line2Line3Line1
Line2
Line3
Welcome.7 * 8 = 56
Bye!a, b, c, d, e
1337
String
*/
/* Desired output:
Line1
Line2
Line3
Line1
Line2
Line3
Welcome.
7 * 8 = 56
Bye!
a, b, c, d, e
1337
String
*/
return 0;
}
log.h
#pragma once
#include <iostream>
#include <utility>
#include <any>
#include <vector>
#include <string>
struct {
std::any aux;
template<typename T, typename...Args>
static void log(T&& t, Args&&... args) {
log(t);
log(std::forward<Args>(args)...);
if(sizeof...(args) > 1) {
} else {
std::cout << "\n";
}
}
static void log(int arg) {
std::cout << arg;
}
static void log(const char* arg) {
std::cout << arg;
}
static void log(std::vector<std::string> arg) {
for(int i = 0; i < arg.size(); ++i) {
if (i + 1 == arg.size()) {
std::cout << arg[i] << std::endl;
} else {
std::cout << arg[i] << ", ";
}
}
}
template<typename T>
static void log(T&& t) {
if (typeid(t).name() == typeid(aux).name()) {
try {
std::any_cast<std::string>(t);
std::cout /* ANY */<< std::any_cast<std::string>(t) << std::endl;
} catch (const std::bad_any_cast& a) {
//std::cout << a.what() << '\n';
try {
std::any_cast<int>(t);
std::cout /* ANY */<< std::any_cast<int>(t) << std::endl;
} catch (const std::bad_any_cast& b) {
std::cout << a.what() << "\n";
}
}
} else {
std::cout << "THE TYPE (" << typeid(t).name() << ") NOT OVERLOADED" << std::endl;
}
}
} console;
我在 console.log() 调用中需要超过 1 个参数来触发换行符。
如何在评估 if 之前拦截调用以推送参数?
编译命令:
g++ -o test main.cpp -std=c++17
编译完整代码的命令:
g++ -o program main.cpp -lcurl -std=c++17
【问题讨论】:
-
请在问题中包含您的代码的minimal reproducible example
-
请在问题本身中发布
console.log代码。见minimal reproducible example。 -
顺便说一句,您不必强调答案应该是正确的(并且经过测试)。错误的答案通常最终会被否决并删除
标签: c++ parameter-passing c++17 function-call intercept