小编Nav*_*K N的帖子

使用g ++创建静态库的优化和标志

我刚刚开始使用Linux上的g ++编译器,并对编译器标志有一些疑问.他们是这样的

优化

我阅读了有关优化标志的内容-O1,-O2-O3在g ++手册页中.我不明白何时使用这些标志.通常你使用什么优化级别?g ++手册说明了以下内容-O2.

优化甚至更多.GCC几乎执行所有支持的优化,不涉及空速 - 权衡.指定-O2时,编译器不执行循环展开或函数内联.与-O相比,此选项增加了编译时间和生成代码的性能.

如果它没有进行内联和循环展开,那么如何实现所述性能,是否建议使用此选项?

静态库

如何使用g ++创建静态库?在Visual Studio中,我可以选择一个类库项目,它将被编译成"lib"文件.什么是g ++中的等价物?

c++ optimization g++ compiler-flags

30
推荐指数
3
解决办法
7万
查看次数

将一个结构指针转换为另一个 - C

请考虑以下代码.

enum type {CONS, ATOM, FUNC, LAMBDA};

typedef struct{
  enum type type;
} object;

typedef struct {
  enum type type;
  object *car;
  object *cdr;
} cons_object;

object *cons (object *first, object *second) {
  cons_object *ptr = (cons_object *) malloc (sizeof (cons_object));
  ptr->type = CONS;
  ptr->car = first;
  ptr->cdr = second;
  return (object *) ptr;
}
Run Code Online (Sandbox Code Playgroud)

cons函数中,变量ptr是类型cons_object*.但是在返回值中它被转换为类型object*.

  1. 我想知道这是怎么可能的,因为cons_objectobject是不同的结构.
  2. 做这样的事情有什么问题吗?

有什么想法吗!

c struct casting

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

基于策略的设计和最佳实践 - C++

struct InkPen
{
  void Write()
  {
    this->WriteImplementation();
  }

  void WriteImplementation()
  {
    std::cout << "Writing using a inkpen" << std::endl;
  }

};

struct BoldPen
{
  void Write()
  {
    std::cout << "Writing using a boldpen" << std::endl;
  }
};

template<class PenType>
class Writer : public PenType
{
public:
  void StartWriting()
  {
    PenType::Write();
  }
};

int main()
{
  Writer<InkPen> writer;
  writer.StartWriting();
  Writer<BoldPen> writer1;
  writer1.StartWriting();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我将上述代码编写为学习基于策略的设计的一部分.我对上面的代码几乎没有疑问

1 - 此实现看起来是否正确?我的意思是:它真的看起来像一个基于政策的设计吗?

2 - 我现在可以将任何类型的笔钩到作家身上.但是当我拿到没有默认构造函数的笔(仅参数化构造函数)时,我会怎么做?我该如何处理这种情况?

template<class PenType>
class Writer : public PenType …
Run Code Online (Sandbox Code Playgroud)

c++ design-patterns policy-injection policy-based-design

26
推荐指数
4
解决办法
3万
查看次数

带有发布和调试版本的简单makefile - 最佳实践

我是makefiles的新手.我从"使用GNU make管理项目"一书中学习了makefile创建和其他相关概念.makefile现在准备好了,我需要确保我创建的那个是好的.这是makefile

#Main makefile which does the build

#makedepend flags
DFLAGS = 

#Compiler flags
#if mode variable is empty, setting debug build mode
ifeq ($(mode),release)
   CFLAGS = -Wall
else
   mode = debug
   CFLAGS = -g -Wall
endif

CC = g++
PROG = fooexe

#each module will append the source files to here
SRC := main.cpp

#including the description
include bar/module.mk
include foo/module.mk

OBJ := $(patsubst %.cpp, %.o, $(filter %.cpp,$(SRC)))

.PHONY:all
all: information fooexe

information:
ifneq ($(mode),release)
ifneq ($(mode),debug)
    @echo "Invalid …
Run Code Online (Sandbox Code Playgroud)

c++ makefile g++

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

自定义类型作为地图的关键 - C++

我试图将自定义类型指定为std :: map的键.这是我用作键的类型.

struct Foo
{
    Foo(std::string s) : foo_value(s){}

    bool operator<(const Foo& foo1) {   return foo_value < foo1.foo_value;  }

    bool operator>(const Foo& foo1) {   return foo_value > foo1.foo_value;  }

    std::string foo_value;
};
Run Code Online (Sandbox Code Playgroud)

当与std :: map一起使用时,我收到以下错误.

error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Foo' (or there is no acceptable conversion) c:\program files\microsoft visual studio 8\vc\include\functional 143
Run Code Online (Sandbox Code Playgroud)

如果我改变下面的结构,一切都有效.

struct Foo
{
    Foo(std::string s) : foo_value(s)   {}

    friend bool …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading stdmap

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

了解返回值优化和返回临时值 - C++

请考虑三个功能.

std::string get_a_string()
{
    return "hello";
}

std::string get_a_string1()
{
    return std::string("hello");
}

std::string get_a_string2()
{
    std::string str("hello");
    return str;
}
Run Code Online (Sandbox Code Playgroud)
  1. RVO是否适用于所有三种情况?
  2. 可以在上面的代码中返回一个临时的吗?我相信它没关系,因为我按值返回它而不是返回任何引用它.

有什么想法吗?

c++ compiler-construction return-value-optimization

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

读取大小为8的无效 - Valgrind + C.

Valgrind Invalid read of size 8在以下代码中报告错误.

我有一个声明的数组,

struct symbol *st[PARSER_HASH_SIZE];
Run Code Online (Sandbox Code Playgroud)

当我的程序初始化时,此数组中的所有元素都被初始化为0.

memset(&st[0], 0, sizeof(st));
Run Code Online (Sandbox Code Playgroud)

我的程序struct symbol根据哈希值创建实例并插入上面的数组.因此,此数组中的少数元素将为NULL,而其他元素将为有效值.

以下代码尝试删除分配的项目和valgrind抱怨行, sym = st[i]; sym != NULL; sym = sym->next

struct symbol *sym = NULL;

/* cleaning the symbol table entries */
for(i = 0; i < PARSER_HASH_SIZE; i++) {
    for(sym = st[i]; sym != NULL; sym = sym->next) { /* <-- Valgrind complains here */
        free(sym);
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图了解这个错误的原因.

任何帮助都会很棒!

c valgrind

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

使用C++头文件的最佳实践

我对头文件的使用有以下疑问.

1 - 在评论后加入警卫

/* Copyright Note and licence information (multiple lines) */
#ifndef FOO_H
#define FOO_H
// Header file contents
#endif
Run Code Online (Sandbox Code Playgroud)

Herb Sutter在他的"C++编码标准"一书中说,像上面这样的代码是有问题的.他说"#ifndef"语句应该出现在头文件的第一行.我觉得这并不令人信服.在头文件中你跟着你们/ gals吗?

2 - 在头文件中使用名称空间

#ifndef FOO_H
#define FOO_H
namespace FooNameSpace{
    // Header file contents
}
#endif
Run Code Online (Sandbox Code Playgroud)

上面的代码是否使用了正确的做法?我的意思是,你在头文件中使用命名空间吗?我知道为什么在头文件中导入命名空间是没有意义的,但是如上所述的声明呢?

如果上面的方法是正确的,那么如何对另一个名称空间中的类进行" 转发声明 "呢?是不是

#ifndef FOO_H
#define FOO_H
namespace AnotherNameSpace{
    class AnotherFoo; // forward declaration
}

namespace FooNameSpace{
    // Use AnotherFoo here
}
#endif
Run Code Online (Sandbox Code Playgroud)

" 前向声明 "是避免" 循环依赖 " 的唯一方法,对吗?

c++ header-files

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

在C#中使用COM互操作时的RCW和引用计数

我有一个使用Office互操作程序集的应用程序.我知道运行时管理的"运行时可调用包装器(RCW)".但我不太确定引用计数如何增加.MSDN说,

RCW只保留对包装的COM对象的一个​​引用,而不管调用它的受管客户端的数量.

如果我理解正确,在以下示例中,

using Microsoft.Office.Interop.Word;

static void Foo(Application wrd)
{
    /* .... */
}

static void Main(string[] args)
{
    var wrd = new Application();
    Foo(wrd);
    /* .... */
}
Run Code Online (Sandbox Code Playgroud)

我将实例传递wrd给另一个方法.但这不会增加内部引用计数.所以我想知道引用计数增加的场景是什么?任何人都可以指出引用计数增加的情况吗?

另外我读了一些博客,说在使用COM对象编程时避免使用双点.像,wrd.ActiveDocument.ActiveWindow.作者声称编译器创建单独的变量来保存将增加引用计数器的值.恕我直言,这是错误的,第一个例子证明了这一点.那是对的吗?

任何帮助都会很棒!

c# com ms-office rcw office-pia

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

当两个重载具有相同的签名时,调用构造函数重载

考虑以下课程,

class Foo
{
    public Foo(int count)
    {
        /* .. */
    }

    public Foo(int count)
    {
        /* .. */
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码无效,不会编译.现在考虑以下代码,

class Foo<T>
{
    public Foo(int count)
    {
        /* .. */
    }

    public Foo(T t)
    {
        /* .. */
    }
}

static void Main(string[] args)
{
    Foo<int> foo = new Foo<int>(1);
}
Run Code Online (Sandbox Code Playgroud)

以上代码有效且编译良好.它调用Foo(int count).

我的问题是,如果第一个无效,第二个如何有效?我知道类Foo <T>是有效的,因为T和int是不同的类型.但是当它像Foo <int> foo = new Foo <int>(1)一样使用时,T会得到整数类型,并且两个构造函数都具有相同的签名吗?为什么编译器不显示错误而不是选择执行重载?

c# compiler-construction constructor overloading

18
推荐指数
3
解决办法
3593
查看次数