小编And*_*ath的帖子

C++ 11:模板参数重新定义默认参数

使用gcc编译以下源代码时,没有错误/警告:

template< typename T = int > T func( );
template< typename T = int > T func( );
Run Code Online (Sandbox Code Playgroud)

当我使用clang ++编译相同的源代码时,我收到以下错误:

redeftempparam.cc:2:24: error: template parameter redefines default argument
template< typename T = int > T func( );
                       ^
redeftempparam.cc:1:24: note: previous default template argument defined here
template< typename T = int > T func( );
                       ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

要编译的命令

[clang++|g++] -Wall -Werror -std=c++11 redeftempparam.cc
Run Code Online (Sandbox Code Playgroud)

(版本信息:gcc 4.7.2,clang version 3.3(trunk 171722))

我的问题:

是否允许这种重新定义?如果没有:你能指点我C++标准中的适当点吗?

c++ templates default-value c++11

8
推荐指数
1
解决办法
2710
查看次数

模板类中静态字段的初始化列表因clang而失败

以下代码snipplet在g ++和clang ++下工作正常:

// bsp1.cc
class A {
public:
  A(int, char const *);

  int value;
  const char * name;
};

class B {
public:
  static const A many_as[];
};

A const B::many_as[] 
{ { 0, "zero" }, 
    { 1, "one" }, 
    { 2, "two" }, 
    { 3, "three" }, 
    { 77, 0 } };
Run Code Online (Sandbox Code Playgroud)

当我将B类更改为模板时:

// bsp2.cc
class A {
public:
  A(int, char const *);

  int value;
  const char * name;
};

template<typename T>
class B {
public:
  static const …
Run Code Online (Sandbox Code Playgroud)

c++ static templates clang c++11

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

使用std :: initializer_list创建树?

我所拥有的是:

struct ExprTreeNode {
   char c;
   std::vector< int > i;
};

ExprTreeNode tn { '+', { 1, 2, 3, 4 } };
Run Code Online (Sandbox Code Playgroud)

我想写的是:

MyTree t1 { '+', { 1, 2, { '*', { 3, 4, 5 } } } };
MyTree t2 { '*', { { '+', { 77, 88, 99, 111 } }, { '-', { 44, 33 } } } };
Run Code Online (Sandbox Code Playgroud)

我可以自由地定义MyTree类(以及可能的帮助程序类) - 但它应该是tree-ish - 类似于TreeNode内容的运算符和包含子节点的容器(例如std :: vector).

在C++中是否可以使用这样的initializer_list来初始化树状结构?(如果可能的话,提示如何做到这一点会很好.)

c++ initializer-list c++11

4
推荐指数
1
解决办法
263
查看次数

如何从 yaml 文件解析 PodSpec.spec.imagePullSecrets ?

我想使用 go 解析以下结构:

---
prjA:
  user1:
    metadata:
      namespace: prj-ns
    spec:
      containers:
        - image: some-contaner:latest
          name: containerssh-client-image
          resources:
            limits:
              ephemeral-storage: 4Gi
            requests:
              ephemeral-storage: 2Gi
      securityContext:
        runAsGroup: 1000
        runAsNonRoot: true
        runAsUser: 1000
      imagePullSecrets:
        - docker-registry-secret
Run Code Online (Sandbox Code Playgroud)

我用来sigs.k8s.io/yaml解组 YAML:

var userConfig map[string]map[string]kubernetes.PodConfig
err = yaml.UnmarshalStrict(yamlFile, &userConfig)
Run Code Online (Sandbox Code Playgroud)

kubernetes 是从哪里导入的github.com/containerssh/kubernetes。一切正常 - 除了immagePullSecrets给出以下错误:

ERROR unmarshal user config file; error [error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go struct field PodSpec.spec.imagePullSecrets of type v1.LocalObjectReference]
Run Code Online (Sandbox Code Playgroud)

在 go 中指定/解析 an …

yaml go kubernetes

4
推荐指数
1
解决办法
1595
查看次数

替换失败有时是错误的吗?

以下代码snipplet尝试实现'std :: is_constructible <A,int>':

#include <type_traits>

struct A { 
  // A(int);
};

template< typename T >
struct cstr_int
{
  static int mi;

  template< typename C >
  static typename std::enable_if< 
    std::is_same< decltype( T( mi ) ), T >::value, char >::type choose( C * );
  static long choose( ... );

  enum { value = ( sizeof( decltype( choose( & mi ) ) ) == 1 ) };
};

bool const b1 = cstr_int< A >::value;
Run Code Online (Sandbox Code Playgroud)

使用g ++这很好用; 使用clang ++打印以下错误:

tmp/sfinae04.cc:14:29: error: no …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae clang++

2
推荐指数
1
解决办法
833
查看次数