I would like to print : table_name[variable_value]
我想打印:table_name[variable_value]
by giving ONE input : table_name[variable_name]
输入一个:table_name[variable_name]
Let me explain a simpler case with a toy solution based on a macro:
让我用一个基于宏的玩具解决方案来解释一个简单的例子:
int i = 1771;
I can print the variable_name
with
我可以打印variable_name。
#define toy_solution(x) cout << #x ;
定义toy_solution(x) cout < #x;
If I execute
如果我执行
toy_solution(i);
toy_solution(我);
"i" will be printed.
“我”将被打印出来。
Now, imagine there is a well-allocated table T
.
现在,假设有一个分配良好的表T。
I would like to write in the program:
我想在节目单上写:
solution(T[i]);
and to read on the screen "T[1771]"
.
在屏幕上读"T[1771]"
An ideal solution would treat the two cases, that is :
理想的解决办法是处理这两种情况,即:
ideal_solution(i)
would print i
.
ideal_solution(我)我打印。
ideal_solution(T[i])
would print T[1771]
.
ideal_solution(T[我])将打印T[1771]。
It is not important to me to use a macro or a function.
使用宏或函数对我来说并不重要。
Thanks for your help.
谢谢你的帮助。
3 个解决方案
#1
5
#define toy_solution(x, i) cout << #x << "[" << i << "]"
#2
4
I would like to print :
table_name[variable_value]
我想打印:table_name[variable_value]
by giving ONE input :
table_name[variable_name]
输入一个:table_name[variable_name]
well, as you did not understand my comment, I'll say out loud in an answer:
既然你不理解我的评论,我就大声回答:
what you want to do is not possible
You have to choose between either @Alin's solution or @lizusek.
I think that @lizusek's solution is better because you're writing C++ code, so if you can do something that gives the same result than with using macros, you should use plain C++ code.
我认为@lizusek的解决方案更好,因为您编写的是c++代码,所以如果您可以做一些与使用宏相同的结果,那么您应该使用简单的c++代码。
edit: let my try to explain why this is not possible
编辑:让我来解释为什么这是不可能的
so what you want is:
所以你想要的是:
f(T[i]) -> T, i
The only way you could write that so it would make sense in preprocessor is:
你能写出来的唯一的方法在预处理中是有意义的
#define f(T[i]) cout<<#T#<<#i#
but then the preprocessor will give an error, because you can't index an array as a function (even a macro function) parameter:
但是,预处理器会给出一个错误,因为您不能将一个数组作为函数(甚至一个宏函数)进行索引:
test.c:5:12: error: expected comma in macro parameter list
#define f(T[i]) cout<<#T#<<#i#
^
If you try to do the same thing using a C++ function, then it's even more non-sense, as a function call such as:
如果您尝试使用c++函数做同样的事情,那么它就更没有意义了,例如:
toy_solution(t[i]);
would actually be translated to the value t[i]
points to at runtime, so inside the function you'll never be able to known that the given value was actually in an array. So what you want is wrong, and you should stick to good coding practices: using a function and if what you want is:
实际上会被转换为t[i]在运行时指向的值,所以在函数中你永远不会知道给定的值实际上是在数组中。所以你想要的是错误的,你应该坚持好的编码实践:使用一个函数,如果你想要的是:
toy_solution(t[i]);
then use:
然后使用:
toy_solution("t", i);
Possible solutions that you should never use
well, when I say it's not possible, it's that the only solutions I can think off are so twisted that you'd be insane to actually use them in your code… And if you do, I hope I'll never read one of your code or I may become violent :-) That's why I won't show you how or give you any code that could help do what I'm about to tell you.
当我说这是不可能的,这是我唯一能想到解决方案是如此扭曲,你会疯狂的实际代码中使用它们,如果你这样做,我希望我永远不会读你的代码或我可能成为暴力:-)这就是为什么我不会给你或者给你任何代码,可以如何帮助做我要告诉你。
- use a template system
- 使用一个模板系统
You could either write your code using your own template system or use one commonly used for HTML processing to process your source code through it and apply a transformation rule such as:
您可以使用自己的模板系统编写代码,也可以使用一个常用的HTML处理方法来处理源代码,并应用转换规则,例如:
toy_solution(t[i]) -> toy_solution("t", t[i])
it's definitely possible, but it makes your build chain even more complicated and dependant on more tools. C/C++ build toolchain are complicated enough, please don't make it worst.
这是完全可能的,但是它使您的构建链更加复杂并且依赖于更多的工具。C/ c++构建工具链已经够复杂了,请不要让它变得更糟。
Or you code make your own fork of C and of a C compiler to change the syntax rules so what you want becomes possible. Though, I personnally would never use your fork, and then I'd go trolling and flaming about this on HN, deeply regretting to have given you such a bad idea :-)
或者,您可以编写自己的C和C编译器的fork来更改语法规则,从而使您想要的成为可能。不过,我个人永远也不会用你的叉子,然后我会在HN上对这事喋喋不休,为给了你这么坏的主意而深感遗憾:
- use a custom class to encapsulate your arrays in
- 使用自定义类来封装数组
if you do something like:
如果你这样做:
template<T>
class Element {
T value;
List<T> _owner;
[…]
}
template<T>
class List {
Element<T> values[];
std::string _name;
[…]
}
so that when you call the function
所以当你调用这个函数时。
toy_solution(T[i]);
the implementation would look like:
执行情况如下:
void toy_solution(Element<T> e) {
std::cout<<e.get_list_name()<<" "<<e.get_value()<<std::endl;
}
but that's sooo much boilerplate and overhead just to avoid doing a simple function definition that does not look as nice as you dream of, that I find it really stupid to do so.
但这是太多的样板文件和开销,只是为了避免做一个简单的函数定义,看起来不像你想象的那么好,我发现这样做真的很愚蠢。
#3
2
You can write a function as simple as that:
你可以写一个简单的函数:
void solution( std::string const& t, int i) {
std::cout << t << "[" << i << "]";
}
usage:
用法:
int i = 1771;
solution( "T", i);
You can also write a macro, but be aware that this is not type safe. Function should be preferred.
您还可以编写一个宏,但是要注意这不是类型安全的。函数应该优先。
#1
5
#define toy_solution(x, i) cout << #x << "[" << i << "]"
#2
4
I would like to print :
table_name[variable_value]
我想打印:table_name[variable_value]
by giving ONE input :
table_name[variable_name]
输入一个:table_name[variable_name]
well, as you did not understand my comment, I'll say out loud in an answer:
既然你不理解我的评论,我就大声回答:
what you want to do is not possible
You have to choose between either @Alin's solution or @lizusek.
I think that @lizusek's solution is better because you're writing C++ code, so if you can do something that gives the same result than with using macros, you should use plain C++ code.
我认为@lizusek的解决方案更好,因为您编写的是c++代码,所以如果您可以做一些与使用宏相同的结果,那么您应该使用简单的c++代码。
edit: let my try to explain why this is not possible
编辑:让我来解释为什么这是不可能的
so what you want is:
所以你想要的是:
f(T[i]) -> T, i
The only way you could write that so it would make sense in preprocessor is:
你能写出来的唯一的方法在预处理中是有意义的
#define f(T[i]) cout<<#T#<<#i#
but then the preprocessor will give an error, because you can't index an array as a function (even a macro function) parameter:
但是,预处理器会给出一个错误,因为您不能将一个数组作为函数(甚至一个宏函数)进行索引:
test.c:5:12: error: expected comma in macro parameter list
#define f(T[i]) cout<<#T#<<#i#
^
If you try to do the same thing using a C++ function, then it's even more non-sense, as a function call such as:
如果您尝试使用c++函数做同样的事情,那么它就更没有意义了,例如:
toy_solution(t[i]);
would actually be translated to the value t[i]
points to at runtime, so inside the function you'll never be able to known that the given value was actually in an array. So what you want is wrong, and you should stick to good coding practices: using a function and if what you want is:
实际上会被转换为t[i]在运行时指向的值,所以在函数中你永远不会知道给定的值实际上是在数组中。所以你想要的是错误的,你应该坚持好的编码实践:使用一个函数,如果你想要的是:
toy_solution(t[i]);
then use:
然后使用:
toy_solution("t", i);
Possible solutions that you should never use
well, when I say it's not possible, it's that the only solutions I can think off are so twisted that you'd be insane to actually use them in your code… And if you do, I hope I'll never read one of your code or I may become violent :-) That's why I won't show you how or give you any code that could help do what I'm about to tell you.
当我说这是不可能的,这是我唯一能想到解决方案是如此扭曲,你会疯狂的实际代码中使用它们,如果你这样做,我希望我永远不会读你的代码或我可能成为暴力:-)这就是为什么我不会给你或者给你任何代码,可以如何帮助做我要告诉你。
- use a template system
- 使用一个模板系统
You could either write your code using your own template system or use one commonly used for HTML processing to process your source code through it and apply a transformation rule such as:
您可以使用自己的模板系统编写代码,也可以使用一个常用的HTML处理方法来处理源代码,并应用转换规则,例如:
toy_solution(t[i]) -> toy_solution("t", t[i])
it's definitely possible, but it makes your build chain even more complicated and dependant on more tools. C/C++ build toolchain are complicated enough, please don't make it worst.
这是完全可能的,但是它使您的构建链更加复杂并且依赖于更多的工具。C/ c++构建工具链已经够复杂了,请不要让它变得更糟。
Or you code make your own fork of C and of a C compiler to change the syntax rules so what you want becomes possible. Though, I personnally would never use your fork, and then I'd go trolling and flaming about this on HN, deeply regretting to have given you such a bad idea :-)
或者,您可以编写自己的C和C编译器的fork来更改语法规则,从而使您想要的成为可能。不过,我个人永远也不会用你的叉子,然后我会在HN上对这事喋喋不休,为给了你这么坏的主意而深感遗憾:
- use a custom class to encapsulate your arrays in
- 使用自定义类来封装数组
if you do something like:
如果你这样做:
template<T>
class Element {
T value;
List<T> _owner;
[…]
}
template<T>
class List {
Element<T> values[];
std::string _name;
[…]
}
so that when you call the function
所以当你调用这个函数时。
toy_solution(T[i]);
the implementation would look like:
执行情况如下:
void toy_solution(Element<T> e) {
std::cout<<e.get_list_name()<<" "<<e.get_value()<<std::endl;
}
but that's sooo much boilerplate and overhead just to avoid doing a simple function definition that does not look as nice as you dream of, that I find it really stupid to do so.
但这是太多的样板文件和开销,只是为了避免做一个简单的函数定义,看起来不像你想象的那么好,我发现这样做真的很愚蠢。
#3
2
You can write a function as simple as that:
你可以写一个简单的函数:
void solution( std::string const& t, int i) {
std::cout << t << "[" << i << "]";
}
usage:
用法:
int i = 1771;
solution( "T", i);
You can also write a macro, but be aware that this is not type safe. Function should be preferred.
您还可以编写一个宏,但是要注意这不是类型安全的。函数应该优先。