template-variables

non-static template member : possible?

拜拜、爱过 提交于 2020-01-02 07:29:58
问题 Is it possible to create non-static template field in a class? If no, how to workaround? Such fields should be created at compile time as needed. Example I have a lot of B -class, like B1 , B2 , B3 . (In real case, they have more meaningful names.) I want to create a class D that has non-static template function add<BX>() that have to counter++ every time I call it, for each individual BX , for a certain instance of D. (In real case, it does somethings more complex.) Here is a working demo to

Template Alias, Variable Template, and auto type deduction failing to deduce template argument

安稳与你 提交于 2019-12-24 19:35:47
问题 While working on my class declaration I'm having some confusion on how to use alias templates and template variables within in a non class template while trying to use auto type deduction. Signal.h #ifndef SIGNAL_H #define SIGNAL_H #include <cstdint> template<typename T> using TimeSignal = T; using DiscreteTime = TimeSignal<std::uint8_t>; using ContinuousTime = TimeSignal<double>; class Signal { private: template<typename T> static TimeSignal<T> time_; double voltage_; double current_; public

String interpolation in YAML

空扰寡人 提交于 2019-12-17 09:57:03
问题 In Perl I can do something like the following: my $home = "/home"; my $alice = "$home/alice"; Can I do something like the following in YAML: Home: /home Alice: $Home/alice So "Alice" is effectively /home/alice in the end? 回答1: Unfortunately, you're out of luck. To do what you want you'd need to pass in $home from a view file (or wherever) and interpolate it in your yaml entry, which could possibly look something like: Alice: ! '%{home}/Alice' See this StackOverflow Q&A for the detailed answer

Is this an elegant way to overload a static member function that provides the same interface as a non static member function?

本秂侑毒 提交于 2019-12-11 15:59:59
问题 Consider the following non class template that has a template variable and uses template aliasing and auto type deduction. template<typename T> using Type = T; using TypeA = Type<int>; using TypeB = Type<double>; class Foo { private: template<typename T> static Type<T> type_; public: template<typename T> explicit Foo( Type<T> type ) { type_<T> = type; } // non static member template<typename T> auto bar() { return type_<T>; } // static member template<typename T> static auto bar(T _x_ = 0) {

YAML reusable variables with case-specific values

青春壹個敷衍的年華 提交于 2019-12-11 03:49:23
问题 Is it possible in .yml config to have dynamic properties in variables that are set depending on a particular case. For example: MY_VAR: &MY_VAR keys: key2: blahblahblah key3: blahblahblah # only apply this for section2, not section1 section1: var: *MY_VAR section2: # this case needs key3 set, otherwise everything else is the same var: *MY_VAR 回答1: YAML's anchors ( &MY_VAR ) and references ( *MY_VAR ) are in the specification to prevent duplication, but also to allow serialisation of objects

Why Must Specializing Argument be void?

Deadly 提交于 2019-12-10 15:26:58
问题 So yet another question in this saga. Guillaume Racicot has been good enough to provide me with yet another workaround so this is the code I'll be basing this question off of: struct vec { double x; double y; double z; }; namespace details { template <typename T> using subscript_function = double(*)(const T&); template <typename T> constexpr double X(const T& param) { return param.x; } template <typename T> constexpr double Y(const T& param) { return param.y; } template <typename T> constexpr

Can I overload template variables?

旧街凉风 提交于 2019-12-06 01:39:02
问题 I want to declare something like this: template <typename T> constexpr enable_if_t<is_integral_v<T>, int[]> foo = { 1, 2 }; template <typename T> constexpr enable_if_t<is_floating_point_v<T>, int[]> foo = { 10, 20, 30 }; But when I try to I'm getting this error: error: redeclaration of template<class T> constexpr std::enable_if_t<std::is_floating_point<_Tp>::value, int []> foo note: previous declaration template<class T> constexpr std::enable_if_t<std::is_integral<_Tp>::value, int []> foo<T>

Can I overload template variables?

ⅰ亾dé卋堺 提交于 2019-12-04 06:02:00
I want to declare something like this: template <typename T> constexpr enable_if_t<is_integral_v<T>, int[]> foo = { 1, 2 }; template <typename T> constexpr enable_if_t<is_floating_point_v<T>, int[]> foo = { 10, 20, 30 }; But when I try to I'm getting this error : error: redeclaration of template<class T> constexpr std::enable_if_t<std::is_floating_point<_Tp>::value, int []> foo note: previous declaration template<class T> constexpr std::enable_if_t<std::is_integral<_Tp>::value, int []> foo<T> I feel like this should be legal as there will never be more than one foo defined for any given

string.format() with optional placeholders

我的梦境 提交于 2019-11-30 17:54:37
I have the following Python code (I'm using Python 2.7.X): my_csv = '{first},{middle},{last}' print( my_csv.format( first='John', last='Doe' ) ) I get a KeyError exception because 'middle' is not specified (this is expected). However, I want all of those placeholders to be optional. If those named parameters are not specified, I expect the placeholders to be removed. So the string printed above should be: John,,Doe Is there built in functionality to make those placeholders optional, or is some more in depth work required? If the latter, if someone could show me the most simple solution I'd

Does PHP have a feature like Python's template strings?

泄露秘密 提交于 2019-11-30 17:06:51
Python has a feature called template strings . >>> from string import Template >>> s = Template('$who likes $what') >>> s.substitute(who='tim', what='kung pao') 'tim likes kung pao' I know that PHP allows you to write: "Hello $person" and have $person substituted, but the templates can be reused in various sections of the code? You could also use strtr : $template = '$who likes $what'; $vars = array( '$who' => 'tim', '$what' => 'kung pao', ); echo strtr($template, $vars); Outputs: tim likes kung pao I think there are a bunch of ways to do this... but this comes to mind. $search = array('%who%'