小编Jag*_*ath的帖子

在编译期间检测继承

我无法弄清楚为什么这段代码返回false.我有部分专业化的第一个版本.它没用,我尝试了第二个版本.它也没用.

更新:我想检查"Derived"是否公开来自"Base".

更新:

    template<typename TDerived, typename TBase>
    struct Derived_From
    {

    public:
        static void constraints(TBase*, TDerived* ptr) { TBase* b = ptr; ignore(b); }
        Derived_From() { void (*p)(TBase*, TDerived*) = constraints; ignore(p);}
    };
Run Code Online (Sandbox Code Playgroud)

我在Strostrup的主页上找到了上面的代码片段.但是,如果派生类不是从Base公开派生的,它不会让代码编译.

template<class TBase, class TDerived>
struct IsDerived
{
    public:
    enum { isDerived = false };
};


template<class TBase>
struct IsDerived<TBase, TBase>
{
    public:
    enum {  isDerived = true };
};


template<class TBase>
struct IsDerived<TBase&, TBase&>
{
    public:
    enum {  isDerived = true };
};

int main()
{ …
Run Code Online (Sandbox Code Playgroud)

c++

8
推荐指数
2
解决办法
384
查看次数

纯虚函数称为错误

我觉得这很奇怪.在Sample_Base的ctor中,我调用bar(),它在内部调用fun(),这是一个纯虚函数.我得到错误"纯虚函数"调用.哪个好.现在,如果我直接从Sample_Base的ctor调用fun(),我就不会得到那个错误.我在VC++ 2010 Beta 2和Ubuntu 9.10上的g ++ 4.4.1上尝试过它.我同意,除了纯虚拟析构函数之外,为纯虚函数提供实现是没有意义的.但是,我对这种行为感到有些惊讶.

class Sample_Base
{
public:
    Sample_Base()
    {
        bar();
       // fun();
    }
    /* This is code does not throw any error.
    Sample_Base()
    {
       fun();
    }
    */

    void bar()
    {
        fun();
    }
    virtual void fun() = 0;
    virtual ~Sample_Base();
};

Sample_Base::~Sample_Base()
{

}

void Sample_Base::fun()
{
    std::cout << "Sample_Base::fun\n";
}

class Sample_Derived : public Sample_Base
{
public:
    Sample_Derived() : Sample_Base()
    {
        fun();
    }

    void fun()
    {
        std::cout << "Sample_Derived::fun\n";
    }

    ~Sample_Derived()
    {

    }
};
Run Code Online (Sandbox Code Playgroud)

c++

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

Sun Studio 12中的模板编译错误

我们正在迁移到Sun Studio 12.1并使用新的编译器[CC:Sun C++ 5.10 SunOS_sparc 2009/06/03].编译使用早期版本的Sun编译器[CC:Sun WorkShop 6 update 2 C++ 5.3 2001/05/15]编译好的代码时,我遇到了编译错误.

这是我得到的编译错误.

"Sample.cc":错误:无法找到main()中所需的LoopThrough(int [2])匹配项.1检测到错误.***错误代码1.

码:

#include <iostream> 

#define PRINT_TRACE(STR) \
std::cout << __FILE__ << ":" << __LINE__ << ":" << STR << "\n";

template<size_t SZ>
void LoopThrough(const int(&Item)[SZ])
{
    PRINT_TRACE("Specialized version");
    for (size_t index = 0; index < SZ; ++index)
    {
        std::cout << Item[index] << "\n";
    }
}


/*     
    template<typename Type, size_t SZ>
    void LoopThrough(const Type(&Item)[SZ])
    {
        PRINT_TRACE("Generic version");        
    }
 */  



int main()
{
    {
       int arr[] …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization sunstudio overload-resolution

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

临时不会在声明结束时被破坏

这种行为是否始终得到保证?下面的代码使用临时unique_ptr创建一个char*指针.我认为unique_ptr应该在语句结束时被破坏.令我惊讶的是,char*仍然指向有效的记忆.

void Fill(char* str, long len)
{
    for(int i = 0; i < len; ++i)
        str[i] = 'a';
}

char* x = std::unique_ptr<char[]>(new char[100]).get();

Fill(x, 100);

std::cout << x << std::endl;
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

使用线程反转字符串

最近,我在接受采访时被要求使用线程实现字符串反转功能.我想出了下面解决方案的大部分内容.选择与否是一个不同的故事:-).我试图在运行Windows 8消费者预览版的家用PC上运行以下解决方案.编译器是VC11 Beta.

问题是,多线程代码总是比顺序代码慢或慢1毫秒.我给出的输入是一个大小为32.4 MB的文本文件.有没有办法让多线程代码更快?或者给出的输入是否太小而没有任何区别?

编辑

我只写void Reverse(char* str, int beg, int end, int rbegin, int rend);
void CustomReverse(char* str);面试方法.所有其他代码都写在家里.

 template<typename Function>
    void TimeIt(Function&& fun, const char* caption)
    {
        clock_t start = clock();     
        fun();     
        clock_t ticks = clock()-start;     
        std::cout << std::setw(30) << caption          << ": "          << (double)ticks/CLOCKS_PER_SEC << "\n"; 
    }

    void Reverse(char* str)
    {


        assert(str != NULL);
        for ( int i = 0, j = strlen(str) - 1; i < j; ++i, --j)
        {
            if ( …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm multithreading c++11

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

使用powershell替换替换

我想写一个等价的

find -name "*.xml" | xargs grep -l "Search String" 
       | xargs perl -p -i -e 's/Search String/Replace String/g'
Run Code Online (Sandbox Code Playgroud)

在powershell中.这就是我提出的.

Get-ChildItem 'D:\code\cpp\FileHandlingCpp\input - Copy' -Recurse |
Select-String -SimpleMatch $src_str | 
foreach{(Get-Content $_.path) | ForEach-Object { $_ -replace $src_str, $target_str }}
Run Code Online (Sandbox Code Playgroud)

我收到错误"进程无法访问该文件,因为它正被另一个进程使用".所以我想出了多行版本,如下所示.我现在能够替换现在的字符串,除了$ src_str.$ src_str有什么问题?

$src_str="<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>"
$target_str=""
echo $src_str

foreach ($var in (Get-ChildItem 'D:\code\cpp\FileHandlingCpp\input - Copy' -Recurse 
                     | Select-String -SimpleMatch $src_str).Path) 
{    
  (Get-Content $var) | ForEach-Object { $_ -replace $src_str, $target_str }
    | Set-Content $var    
}
Run Code Online (Sandbox Code Playgroud)

unix powershell

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

在angular2中调用typescript getter

我想从angular2模板中调用typescript属性.如果我在循环中调用对象的属性,我不能这样做.如果没有循环,代码工作正常.

从Angular 2类内部模板调用方法,如果没有循环,这个问题可以解决问题.

请在这里找到Plunker .

import {Component} from '@angular/core'

export class User {
  id: number;
  mail: string;
  created_at: string;
  first_name: string;
  last_name: string;
  deleted_at: any;

 // if I call {{user.name}} nothing gets printed.
 get name() {
    return "My name is:" + this.first_name;
  }
}

@Component({
  selector: 'my-app',
  template: `
    <ion-list *ngFor="let user of users">
        <div>{{user.first_name}}</div>
    </ion-list>
  `
})
export class App {
  users: User[];
  constructor() {
    this.users = [ {first_name:"Jagan"}, {first_name:"Nath"}  ];
  }
}
Run Code Online (Sandbox Code Playgroud)

更新

这是一段代码.工作版和非工作版都在下面给出.

//our …
Run Code Online (Sandbox Code Playgroud)

typescript angular

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

将数组存储为关联数组中的值

我有一个问题,我需要一个数组作为关联数组中的值.

浏览下面的代码.在这里,我试图循环一个目录中的文件,更有可能超过1个文件可以具有相同的ctrno.所以,我想看看所有文件都有相同的内容ctrno.下面的代码$ctrno_hash[$ctrno] = @arr;在else条件下给出错误" ".同样的情况也适用于if条件.

我是否采用了正确的方法,还是可以采取不同的方式?

sub loop_through_files
{
    $file = "@_";
    open(INPFILE, "$file") or die $!;
    #print "$file:$ctrno\n";
    while (<INPFILE>)
    {
       $line .= $_;
    }
    if ($line =~ /$ctrno/ )
    {
       print "found\n";
       if ( exists $ctrno_hash[$ctrno])
       {
          local @arr = $ctrno_hash[$ctrno];
          push (@arr, $file);
          $ctrno_hash[$ctrno] =  @arr;
       }
       else
       {
          local @arr;
          push(@arr, $file);
          $ctrno_hash[$ctrno] =  @arr;
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

perl

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

批评这个c ++代码

类似于下面编写的代码存在于生产中.你们有人可以审查它并告诉我这些代码是否一直运行良好.

class Base
{
    public:
        virtual void process() = 0;
};

class ProductA : public Base
{
    public:
    void process()
    {
        // some implementation.
        doSomething();
    }

    void setSomething(int x)
    {

    }

    virtual void doSomething()
    {
         // doSomething.
    }

};

class ProductANew : public ProductA
{
    public:
        ProductANew() : ProductA() { }
        void doSomething()
        {
           // do Something.
        }
};


int main(int argc, char *argv[])
{
    Base* bp = new ProductANew();
    dynamic_cast<ProductA*>(bp)->setSomething(10);
    bp->process();
}
Run Code Online (Sandbox Code Playgroud)

c++

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

为什么这个C++代码比C#variant更慢

最近,我们要求有超过100,000个xml文件,并且所有这些文件都需要修改xml中的特定数据.简单的perl命令可以完成这项工作,但perl没有安装在文件所在的机器上.因此我写了一个小的C#代码来完成这项工作.

private static void ModifyXML(string[] args)
{
      Stopwatch sw = new Stopwatch();
      sw.Start();
      string path = @args[0];
      string opath = @args[1];
      string token = "value_date=\"20121130\"";
      string target = "value_date=\"20121019\"";

      Parallel.ForEach(Directory.EnumerateFiles(path), (file) =>
      {
           StringBuilder sb = new StringBuilder(File.ReadAllText(file));
           sb.Remove(0, 55);
           sb.Replace(token, target);
           var filename = file.Split(new char[] { '\\' }).Last();                
           File.WriteAllText(string.Format("{0}\\{1}", opath, filename), sb.ToString());
       });
       TimeSpan ts = sw.Elapsed;
       Console.WriteLine("Took {0} secs", ts.TotalSeconds);
}
Run Code Online (Sandbox Code Playgroud)

我决定实现C++版本.事实证明,C++版本并没有明显快于C#版本.在两个版本中运行了一段时间.实际上,它在某些运行期间和C#版本一样快.

对于C#,我使用的是.NET 4.0,而对于C++,它使用的是VC10.

void FileHandling(std::string src, std::string dest)
{
     namespace fs = boost::filesystem;
    auto start = …
Run Code Online (Sandbox Code Playgroud)

c# c++ ppl

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

C++17类模板推导

我正在尝试在 C++17 中的类模板推导下。
我编写了一个示例类模板,可以在不指定模板类型的情况下进行构建。std::unique_ptr不能在不指定类型的情况下构造。
我需要帮助来理解为什么会这样。

使用 clang 5.0 编译的代码

// Please don't worry about memory leaks, etc. This is sample code anyways.
template<typename T, typename deleter = std::default_delete<T>>
struct Sample
{
T* x_;
deleter func_;

Sample(T* x = nullptr, deleter func = deleter{})
: x_(x)
, func_(func)
{
}
};

auto sample = Sample(new int(10));
std::cout << *(sample.x_) << '\n';
Run Code Online (Sandbox Code Playgroud)

下面的代码无法编译。

auto ptr = std::unique_ptr(new int(10)); 
Run Code Online (Sandbox Code Playgroud)

c++ c++17

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