MediaWiki: Using a template result as an internal wiki link parameter - templates

In this explanation, the following variables are used:
A: Page address
B: Page name
C: Category Name
D: Variable. Value is 'A'
T: Template
V: Variable
Using MediaWiki, the syntax for using a link is [[C/A|B]]. This creates a link to page A, which is inside category C, with text B. The syntax for using a template inside another template with a variable as an argument is {{T|{{{V|#}}}}} This produces the result of template T with variable V. Suppose this result is exactly A. Now, combining these approaches, I expect the code [[C/{{T|{{{V|#}}}}}|B]] to produce the same link as the link syntax example. However, the result is:
[[C/A|B]]
E.g. exactly the code that needs to be evaluated. Note how the template does get evaluated correctly, but the link is not parsed. It seems like I need to 'reverse' the priority some way.
Side note: My approach does work with just variables. E.g. if variable D were to hold the value A, the code [[C/{{{D|#}}}|B]] does produce the correct link. MediaWiki documentation is surprisingly sparse on the subject.
Hint: Wikipedia itself has a number of link templates that can be used as examples, however these form a complex interwoven web from which it is hard to interpret the correct notation for this particular case.
Questions:
How do I properly call a template inside of this context?
What about using the template result for other parts of the link,
such as the text?
What if this is a linked image or other special link?
Other things:
Preferrably, the solution should involve the use of only a single template if this is possible. If a 'dummy' link template containing a simple [[{{{1}}}]] is required, is there a default MediaWiki template that can do this, reducing clutter?

Related

Placeholder variable type

I need to get information from a class function. Said class has overloaded operators for basically any standard type. Therefore
double foo = exampleObject.getInformation();
and
std::string faa = exampleObject.getInformation();
Would both work. If the information can not be transformed into a double, foo will be set to 0. The initialization of faa will always work. (It can always be expressed as a string)
My problem is: I want to get the information and save it as a double variable, if that can not be done as the information is not of numeric type, I want the variable to be a string. I basically need a variable that can change its type. How do I do this? I'm sorry if that is a very basic question, C++ is not my main programming language.
Have you tried using Function Templates?
They won't change the type of a variable but will allow you to write your code in a way that works with more than 1 data type.
If c++ is not your main, I would recommend checking the checking the documentation for Function Templates on cplusplus.com
Here => https://cplusplus.com/doc/oldtutorial/templates/

c++ auto for type and nontype templates

In c++17 template <auto> allows to declare templates with arbitrary type parameters. Partially inspired by this question, it would be useful to have an extension of template <auto> that captures both type and nontype template parameters, and also allows for a variadic version of it.
Are there plans for such an extension in the next c++20 release? Is there some fundamental problem in having a syntax like template<auto... X>, with X any type or nontype template parameter?
Are there plans for such an extension in the next c++20 release?
No.
Is there some fundamental problem in having a syntax like template<auto... X>, with X any type or nontype template parameter?
It would be a totally new concept in the language - having a name refer to either a type or a value in the same place. So it'd come with all sorts of additional questions - and probably additional language features to check if X is a type or not.
The syntax likely cannot be template <auto... X> struct Y { }; since that syntax already has meaning and means a bunch of values and Y<int>{} is ill-formed.
There are definitely places where such a thing would be useful though. A proposal would just have to address these issues.
The big issue with trying to do something like that is grammar. Template parameters state up-front whether they are templates, types, or values, and the most important reason for this is grammatical.
C++ is a context-sensitive grammar. That means that you cannot know, just from a sequence of tokens, what a particular sequence of tokens means. For example, IDENTIFIER LEFT_PAREN RIGHT_PAREN SEMICOLON. What does that mean?
It could mean to call a function named by IDENTIFIER with no parameters. It could mean to default initialize a prvalue of a class named by IDENTIFIER. These are rather different things; you might conceptually see them as similar, but C++'s grammar does not.
Templates are not macros; they're not doing token pasting. There is some understanding that a piece of code in a template is supposed to mean a specific thing. And you can only do that if you at least know what kind of thing a template parameter is.
In order to retain this ability, these "omni template parameters" cannot be utilized until you actually know what they mean. So in order to create such a feature in C++, you would need to:
Create a new syntax to declare omni template parameters (auto isn't going to fly, as it already has a specific meaning).
Provide a syntax for determining what kind of thing an omni template parameter is.
Require the user to invoke that syntax before they can use such parameter names in most ways. This would typically be via some form of specialized if constexpr block, but pattern matching proposals represent an interesting alternative/additional way to handle them (since they can be expressions as well as statements). And expansion statements represent a possible way to access all of the omni parameters in a parameter pack.
I can't see how it would be useful that a template argument could be dynamically either a type or a value? The code statements that use types are very different to those that which use constant values introduced through the template argument.
The only way would be a big "if constexpr" which would make it pointless in my view.
Ok, having looked more closely at the referenced question, I guess there is room there for generically pass-through wrapping the various explicit base template implementations that use different parameter orderings. I still fail to see a huge benefit. The compiler errors when it goes wrong are going to be unfathomable, if nothing else!
I remember being told that overloading and templates were going to rid the world of the unfathomable error messages generated from macros. I have yet to see it!

How to read number of arguments - c++

Usually in my code I need to use specific functions for various variables i.e.
object->SetStatus("var1",1); object->SetAddress("var1",&var1);
object->SetStatus("var2",1); object->SetAddress("var2",&var2);
object->SetStatus("var3",1); object->SetAddress("var3",&var3);
...
My idea is to use a function that will do this automatically by calling it, i.e.
object->function(var1,var2,var3,...);
To achieve that I have to solve 3 issues
I need to read the number of arguments when calling function()
I need to parse somehow the argument names inside the code
Since the variables are not of the same type, I need to find a way to make function() type "transparent"
Since I am newbie in c++ coding, I tried to search fo something similar, but I couldn't find anything.
Any help, advice or remark is more than welcome!
There are multiple ways to do so. One way is make a Base class and all your variable type will inherit from this base class. Then pass a map<string,Base> as an argument to you function. name of variable will be key and value will be actual variables. Iterate through the map and set and assign values to methods.
You could consider some variadic template, if coding in C++11 or C++14. There is considerable literature about that subject (e.g. this tutorial), which is a bit tricky (so explaining it here is not reasonable). Read also about parameter pack
You could also use C style varargs using <cstdarg>
Perhaps std::initializer_list could be useful too.

Template Parameters of the version Type

So there are 3 types of template parameters:
type
non-type
template
SO if the use of templates is to serve as a "class" to classes...in other words a template is to a class what a class is to an object... and provide things such as data type independence, why would I want to use a type parameter template?
for example, why would I ever want to use something like
template <int n = 1>
?
Thank you
There are plenty of uses, C++ templates are not just a mean to "get away with types".
std::array<T, N> is one example : capture the array size at compile time
Many examples in the standard library : see std::get<> to access tuple elements
Integrals arguments can server as an input for MTP algorithms (and consequently, allow C++ templates to form a complete Turing machine, which is a major property of the language ):
Example of a compile-time factorial :
template <int n>
struct factorial {
enum { value = n * factorial<n - 1>::value };
};
template <>
struct factorial<0> {
enum { value = 1 };
};
As I understand your question, you have a relationship like:
(meta-template)->template->class->object
Meta templates are templates which can act as a kind of template generator. This is not really the truth but is also not really the truth that a template is a class generator.
Templates can be used as a class generator. This is what often results in multiple copies of the same code in the binary and is sometimes not what is really useful.
But a template can do much more!
Templates allow calculations during compile time. They maybe did not generate code and they will not result in any class and also not in an instance. The complete code is evaluated during compile time and results in new types which itself can be used as traits for other templates or they can calculate constants which can be used as normal values in the code. All this without any template->class->object relation.
And this is the place where sometimes integer values come in use. They can be used to do compile time calculations. And also this values can be used as traits for selecting specialization.
So simply your rule is only one facet of the c++ template world. And also in this little world, an integer can be used to create classes with this int parameters, like fixed sized arrays and others. See std::array for example where a int value for the size is a template parameter.
Another use case for int parameters in templates are recursive templates which runs over types. In this case often a counting int parameter can be used. Maybe this will be evaluated like a switch in compile time and the counter in compile time is something like an enum in the run time world.
Hope this helps.
Your question is confusing. You claim to ask:
why would I want to use a type parameter template?
However your example is an example of using a non-type template parameter.
You would want/need to use a non-type template parameter (as in your example) when the value-in-question must be a compile time constant.
For example, the second template parameter of std::array affects its size, which must be a compile time constant, and therefore can only be affected by compile time constants. Therefore you cannot specify this at run time, and therefore it must be specified via template parameter at compile time.
You can see an example here:
https://android.googlesource.com/platform/art/+/master/compiler/utils/arena_allocator.h
ArenaAllocatorStatsImpl is defined as (line 63):
template class ArenaAllocatorStatsImpl;
where kCount means that you want to count allocations if it's true.
Then you have an implementation (line 66) of version which doesn't count allocations
template <> class ArenaAllocatorStatsImpl
In line 82 there is a generic implementation but it's used only for the case where kCount is true because false is covered by implementation in line 66.
This allows you to have two version of code. One for debugging and developing and other for production. Templates are resolved at compile time so you have no overhead on production.

Reduce C++ source by variable substitution and dead code truncation?

Suppose I have some template classes with Nontype parameters.
template <int hi, int wid>
class SomeThing {
...
}
I need to create a tool to reduce this source with given value of hi and wid, say, hi=2; wid=3. Sequentially, there might be some code becomes dead code, and the tool also needs to truncate them away. So, finally I expect to see a reduced source code as the output of the tool. Is there any known way to do this? A harder way may be to create my own c++ parser... sounds terrible even a simplified one.
I know there are tools like gcc-xml and clang which can parse it and give an easy-to-parse intermediate file. However, it looks like that it's not enough for me to regenerate c++ source file from that.
[EDIT]
A whole picture is to create a tool to generate source code from source code, with variable substitution and dead code truncation.
I'm not sure I quite understood your question, but would template specialization answers your needs?
template<>
class SomeThing<2, 3> {
//trimmed content
};
If you instantiate SomeThing with the value 2 and 3, the specialization will be chosen by the compiler and the generated executable will contain only the "truncated" content.
Edit:
Based on your edit, I suspect that you'd like to have a partial evaluator for C++, meaning a program which takes a program and some of its inputs, and generates a specialized version of the program where all that could be evaluated had been evaluated.
I'm not aware of any existing implementations for native C++; however, you can find partial evaluators for many functional languages, but also for Pascal and C. Some works have been done to create a partial evaluator for the .Net bytecode (MSIL), which could be used to partially evaluate C++/CLI. [Chepovsky et al. 2003]
The C++ template mechanism can be seen as a limited kind of partial evaluation, since the compiler generates code specialized (and potentially partially evaluated) with the template parameters. However, all this is performed by the compiler internally, there is no intermediary C++ source code that you could visualize. However, you can have a look at the generated assembly code, which gives you a good idea of the operations/evaluations/optimizations performed by the compiler during template instantiation.
There's no clean way to do this, as template code is generally Turing-complete.
As a very simple example, consider
template<int I>
class X : public X<I/2>
{
};
Now say that you want to reduce this for I==351. What exactly should be the base classes be? For real-world code you will need a full C++ compiler. Worse, you will also need a matching standard library implementation, and one that is fully representative of all compliant standard library implementations (!!)
Consider the following code:
template <int I>
class X : public std::vector<X<I/2> >
{
// Methods
};
Dead code elimination will depend on the implementation of std::vector. If your implementations differ, you can accidentily eliminate code that is in fact needed.