标签: template-variables

在yaml中使用占位符

有没有办法在yaml中使用占位符,如下所示:

foo: &FOO
    <<propname>>: 
        type: number 
        default: <<default>>

bar:
    - *FOO 
       propname: "some_prop"
       default: "some default" 
Run Code Online (Sandbox Code Playgroud)

yaml indirection computed-properties template-variables

26
推荐指数
2
解决办法
6万
查看次数

PHP有像Python的模板字符串这样的功能吗?

Python有一个称为模板字符串的功能.

>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
Run Code Online (Sandbox Code Playgroud)

我知道PHP允许你写:

"Hello $person"
Run Code Online (Sandbox Code Playgroud)

并已$person替换,但模板可以在代码的各个部分重用?

php string templates template-variables

23
推荐指数
5
解决办法
2万
查看次数

带有可选占位符的string.format()

我有以下Python代码(我使用的是Python 2.7.X):

my_csv = '{first},{middle},{last}'
print( my_csv.format( first='John', last='Doe' ) )
Run Code Online (Sandbox Code Playgroud)

我得到一个KeyError例外,因为没有指定'middle'(这是预期的).但是,我希望所有这些占位符都是可选的.如果未指定这些命名参数,我希望删除占位符.所以上面打印的字符串应该是:

John,,Doe
Run Code Online (Sandbox Code Playgroud)

是否有内置功能使这些占位符可选,或者是否需要更深入的工作?如果是后者,如果有人能告诉我最简单的解决方案,我会很感激!

python string-formatting template-variables

17
推荐指数
3
解决办法
1万
查看次数

链接器错误与变量模板

请考虑以下代码:

#include <iostream>

template<typename T>
T n;

int main()
{
    n<int> = 42;
    std::cout << n<int> << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

编译并与g ++ 5.1链接,并显示42.但是,clang ++ 无法链接它:

对n <int>的未定义引用

如果我初始化模板变量,如

template<typename T> T n{};
Run Code Online (Sandbox Code Playgroud)

然后clang ++也链接它.

知道发生了什么事吗?如果没有链接程序,clang ++"是否正确"?如果我初始化模板变量,为什么它可以工作?

据我所知,模板变量只是围绕静态成员的模板包装器的语法糖,因此n<int> = 42有效地专门化了int实例.IMO,代码应链接.

c++ linker templates c++14 template-variables

11
推荐指数
1
解决办法
156
查看次数

YAML中的字符串插值

在Perl中,我可以执行以下操作:

my $home = "/home";
my $alice = "$home/alice";
Run Code Online (Sandbox Code Playgroud)

我可以在YAML中执行以下操作:

Home: /home
Alice: $Home/alice
Run Code Online (Sandbox Code Playgroud)

那么"爱丽丝" /home/alice到底有效吗?

yaml string-interpolation template-variables

9
推荐指数
3
解决办法
2万
查看次数

成员模板变量专门化

一个类可以包含一个必须是静态的成员模板变量:

class B
{   
    public:
        template <typename X>
            static X var;

        B() { std::cout << "Create B " << __PRETTY_FUNCTION__ << std::endl; }

        template <typename T>
        void Print() { std::cout << "Value is " << var<T> << std::endl; }
};
Run Code Online (Sandbox Code Playgroud)

它必须在类范围之外声明所有静态成员:

以下编译并按预期工作:

 template<typename T> T B::var=9; // makes only sense for int,float,double...
Run Code Online (Sandbox Code Playgroud)

但是如何将这样的var专门化为以下非工作代码(使用gcc 6.1的错误消息):

template <> double B::var<double>=1.123; 
Run Code Online (Sandbox Code Playgroud)

失败:

main.cpp:49:23: error: parse error in template argument list
 template <> double B::var<double>= 1.123;
                       ^~~~~~~~~~~~~~~~~~
main.cpp:49:23: error: template argument 1 is invalid …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization c++14 template-variables

6
推荐指数
1
解决办法
172
查看次数

g ++和clang ++使用变量模板和SFINAE的不同行为

另一个问题是"g ++和clang ++之间谁是对的?" 对于C++标准大师.

假设我们希望将SFINAE应用于变量模板,以便仅在模板类型满足特定条件时启用变量.

例如:启用barif(且仅当)模板类型具有foo()给定签名的方法.

通过具有默认值的其他模板类型使用SFINAE

template <typename T, typename = decltype(T::foo())>
static constexpr int bar = 1;  
Run Code Online (Sandbox Code Playgroud)

适用于g ++和clang ++,但有一个问题:可以被劫持,解释第二个模板类型

所以

int i = bar<int>;
Run Code Online (Sandbox Code Playgroud)

给出了编译错误

int i = bar<int, void>;
Run Code Online (Sandbox Code Playgroud)

编译没有问题.

所以,从我对SFINAE的无知的底部,我尝试启用/禁用相同变量的类型:

template <typename T>
static constexpr decltype(T::foo(), int{}) bar = 2; 
Run Code Online (Sandbox Code Playgroud)

惊喜:这对g ++有用(编译)但是clang ++不接受它并给出以下错误

tmp_003-14,gcc,clang.cpp:8:30: error: no member named 'foo' in 'without_foo'
static constexpr decltype(T::foo(), int{}) bar = 2;
                          ~~~^
Run Code Online (Sandbox Code Playgroud)

像往常一样,问题是:谁是对的?g ++或clang ++?

换句话说:根据C++ 14标准,SFINAE可以用于变量模板的类型吗?

以下是一个完整的例子

#include <type_traits>

// works with both …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae c++14 template-variables

6
推荐指数
1
解决办法
196
查看次数

我可以重载模板变量吗?

我想声明如下内容:

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 };
Run Code Online (Sandbox Code Playgroud)

但是当我尝试出现此错误时

错误:template<class T> constexpr std::enable_if_t<std::is_floating_point<_Tp>::value, int []> foo
注释的重新声明:先前的声明template<class T> constexpr std::enable_if_t<std::is_integral<_Tp>::value, int []> foo<T>

我觉得这应该是合法的,因为foo对于任何给定的模板参数,永远不会定义多个。有什么我可以帮助编译器理解的吗?

c++ templates overloading enable-if template-variables

6
推荐指数
1
解决办法
142
查看次数

模板变量C数组完全专业化应该指定数组大小吗?

我正在尝试实现具有以下特征的模板化C数组:

// template definition
template< int a, int b > constexpr int    arr[]       = { 1 };

// partial specialization, works ok
template< int b >        constexpr double arr<0, b>[] = { 2.0 };

// full specialization, compile error in MSVC, ok in g++
template< >              constexpr float  arr<1, 0>[] = { 3.0f };
Run Code Online (Sandbox Code Playgroud)

我将Visual Studio 2017和MSVC编译器与C ++标准设置为C ++ 17一起使用,并且编译器抱怨了C2133: 'arr<1,0>': unknown size,因此将大小添加1到完整的专业化解决了该错误。但是,它会在带有-pedantic标志的Ubuntu g ++ 8.1.0下进行编译。

我认为,对函数和类的完全专业化的行为就好像定义了非模板版本一样,因此我想这也应适用于变量模板,并且上面的完全专业化可以等同于(名称除外)

constexpr float arr_with_a1_and_b0[] = { 3.0f }; …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer list-initialization template-variables c++17

5
推荐指数
1
解决办法
141
查看次数

解决带有嵌套模板变量的Visual Studio内部编译器错误

我正在尝试编写可使我索引到函数的参数类型的代码:

template <typename R, typename... ARGS>
R function_return(R(*)(ARGS...));

template <typename R, typename... ARGS>
std::tuple<ARGS...> function_parameters(R(*)(ARGS...));

template <int I, typename T>
using get_type = typename std::conditional_t<(I < 0), std::tuple_element<static_cast<int>(std::tuple_size_v<T>) + I, T>, std::tuple_element<I, T>>::type;

template <int I, typename T>
using parameter_type = get_type<I, decltype(function_parameters(std::declval<T>()))>;
Run Code Online (Sandbox Code Playgroud)

Live Example (ICE under VS) Live Example (working on GCC)

但是当我尝试在上使用它时,出现内部编译器错误:

严重错误C1001:编译器中发生内部错误。

我还有另一种方法可以解决内部编译器错误吗?

c++ function-parameter internal-compiler-error template-variables visual-studio-2017

5
推荐指数
1
解决办法
110
查看次数