metaprogramming

Passing an integer or a type as a template parameter?

邮差的信 提交于 2021-02-19 03:12:58
问题 Here is an example case of what I'm trying to do (it is a "test" case just to illustrate the problem) : #include <iostream> #include <type_traits> #include <ratio> template<int Int, typename Type> constexpr Type f(const Type x) { return Int*x; } template<class Ratio, typename Type, class = typename std::enable_if<Ratio::den != 0>::type> constexpr Type f(const Type x) { return (x*Ratio::num)/Ratio::den; } template</*An int OR a type*/ Something, typename Type> constexpr Type g(const Type x) {

Passing an integer or a type as a template parameter?

冷暖自知 提交于 2021-02-19 03:12:49
问题 Here is an example case of what I'm trying to do (it is a "test" case just to illustrate the problem) : #include <iostream> #include <type_traits> #include <ratio> template<int Int, typename Type> constexpr Type f(const Type x) { return Int*x; } template<class Ratio, typename Type, class = typename std::enable_if<Ratio::den != 0>::type> constexpr Type f(const Type x) { return (x*Ratio::num)/Ratio::den; } template</*An int OR a type*/ Something, typename Type> constexpr Type g(const Type x) {

Passing an integer or a type as a template parameter?

做~自己de王妃 提交于 2021-02-19 03:10:50
问题 Here is an example case of what I'm trying to do (it is a "test" case just to illustrate the problem) : #include <iostream> #include <type_traits> #include <ratio> template<int Int, typename Type> constexpr Type f(const Type x) { return Int*x; } template<class Ratio, typename Type, class = typename std::enable_if<Ratio::den != 0>::type> constexpr Type f(const Type x) { return (x*Ratio::num)/Ratio::den; } template</*An int OR a type*/ Something, typename Type> constexpr Type g(const Type x) {

How to generate nested loops at compile time

亡梦爱人 提交于 2021-02-17 09:14:37
问题 I have an integer N which I know at compile time. I also have an std::array holding integers describing the shape of an N -dimensional array. I want to generate nested loops, as described bellow, at compile time, using metaprogramming techniques. constexpr int N {4}; constexpr std::array<int, N> shape {{1,3,5,2}}; auto f = [/* accept object which uses coords */] (auto... coords) { // do sth with coords }; // This is what I want to generate. for(int i = 0; i < shape[0]; i++) { for(int j = 0; j

Conditional compilation and non-type template parameters

旧街凉风 提交于 2021-02-15 06:18:08
问题 I am having trouble understanding non-type template arguments and was hoping someone could shed light on this. #include <iostream> template<typename T, int a> void f() { if (a == 1) { std::cout << "Hello\n"; } else { T("hello"); } } int main() { f<int, 1>; } When I compile this, I get an error saying: /tmp/conditional_templates.cc:13:12: required from here /tmp/conditional_templates.cc:8:5: error: cast from ‘const char*’ to ‘int’ loses precision [-fpermissive] T("hello"); ^ But, can't the

Why can I evaluate a function receiving a std::pair at compile-time, but not assert it? [duplicate]

依然范特西╮ 提交于 2021-02-11 14:13:59
问题 This question already has answers here : constexpr function parameters as template arguments (4 answers) C++11 constexpr function pass parameter (3 answers) Closed 12 months ago . I wrote a function that receives a variadic number of std::pairs. It takes the pairs, subtracts the 2nd element against the 1st element of each pair, and returns a tuple of the newly generated values like this: #include <tuple> #include <utility> template<typename... pairs> inline constexpr auto foo(pairs&& ...p)

Julia: inject code into function

浪子不回头ぞ 提交于 2021-02-10 04:28:33
问题 I would like to inject code into a function. For concreteness, consider a simple simulater: function simulation(A, x) for t in 1:1000 z = randn(3) x = A*x + z end end Sometimes I would like to record the values of x every ten time-steps, sometimes the values of z every 20 time-steps, and sometimes I don't want to record any values. I could, of course, put some flags as arguments to the function, and have some if-else statements. But I would like to rather keep the simulation code clean, and

Julia: inject code into function

岁酱吖の 提交于 2021-02-10 04:28:27
问题 I would like to inject code into a function. For concreteness, consider a simple simulater: function simulation(A, x) for t in 1:1000 z = randn(3) x = A*x + z end end Sometimes I would like to record the values of x every ten time-steps, sometimes the values of z every 20 time-steps, and sometimes I don't want to record any values. I could, of course, put some flags as arguments to the function, and have some if-else statements. But I would like to rather keep the simulation code clean, and

creating class properties dynamically

北城余情 提交于 2021-02-08 14:10:56
问题 I am looking for a way to dynamically create classes with specific properties accessible via typical instance notation. DynoOne = createClass('DynoOne',props=['A','B']) d = DynoOne (database='XYZ') d.A = d.B + 1 DynoTwo = createClass('DynoTwo',props=['A','C','E']) q = DynoTwo (database='QRS') q.A = q.C + 2*q.E Details of how the "props" are actually acquired and modified would be hidden. This also makes it easier to add access to new props as they become available. I have experimented with

What makes a CanvasRenderingContext2D a CanvasRenderingContext2D?

冷暖自知 提交于 2021-02-08 07:57:42
问题 Consider the following web page. <html> <body> <canvas id="canvas" width="300" height="300" style="border:1px solid #000000;"> </canvas> </body> </html> I open this page in Firefox, open the JS console and type the following. > document.getElementById("canvas").getContext("2d") The output is as follows: CanvasRenderingContext2D { canvas: canvas#canvas, mozCurrentTransform: (6) […], mozCurrentTransformInverse: (6) […], mozTextStyle: "10px sans-serif", mozImageSmoothingEnabled: true,