小编sme*_*lin的帖子

C++模板和内联

当我编写一个简单的(非模板)类时,如果函数实现是"正确"提供的,它会自动被视为inline.

class A {
   void InlinedFunction() { int a = 0; }
   // ^^^^ the same as 'inline void InlinedFunction'
}
Run Code Online (Sandbox Code Playgroud)

在谈论基于模板的类时,这条规则怎么样?

template <typename T> class B {
   void DontKnowFunction() { T a = 0; }
   // Will this function be treated as inline when the compiler
   // instantiates the template?
};
Run Code Online (Sandbox Code Playgroud)

此外,该inline规则如何应用于非嵌套模板函数,例如

template <typename T> void B::DontKnowFunction() { T a = 0; }

template <typename T> inline void B::DontKnowFunction() { T a = 0; } …
Run Code Online (Sandbox Code Playgroud)

c++ templates inline-method

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

调用基类的运算符......安全吗?

以下模式确定/安全吗?或者有任何缺点吗?(我也将它用于相等运算符)

Derived& operator=(const Derived& rhs)
{
    static_cast<Base&>(*this) = rhs;
    // ... copy member variables of Derived
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

c++ operators

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

QT4:带圆角的透明窗口

如何创建具有圆形边框(无标准边框)的部分透明窗口?

(我Qt::FramelessWindowHint以前禁用标准边框)

我试过样式表,但border-radiusopacity似乎不把窗户上的任何效果,它只能在封闭的小部件的孩子.

我的第二个想法是使窗口完全透明(带setWindowOpacity),然后添加一个带圆角的附加小部件(因为对子节点border-radius起作用),然后将我的所有其他小部件分组到该小部件中.但这不起作用,因为setWindowOpacity影响所有孩子(我还没有找到改变这种行为的方法).

任何使外窗口透明的方法我都能想到(比如样式表opacity)不能正常工作(我只得到一个黑盒而不是透明窗口)

任何帮助将受到高度赞赏.

c++ user-interface qt qt4

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

Python的C扩展中的Docstrings?

在为Python创建C扩展时,是否有可能以某种方式将扩展为文档字符串的注释写入扩展的用户?

python cpython python-c-extension

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

如何在Visual Studio C++ Express版中启用自动完成功能?

请指导我,如何在VS C++中启用自动完成功能?通过自动完成,我的意思是,当我在控件名称后面放一个点时,编辑器应该显示一个下拉菜单供选择.

谢谢.

c++ autocomplete visual-studio

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

close函数无法识别 - C++

我在一个名为的文件中有以下代码RaftLog.cc:

#include <algorithm>
#include <fcntl.h>
#include <sys/stat.h>



namespace LogCabin {
namespace Server {
namespace RaftConsensusInternal {

namespace FilesystemUtil = Storage::FilesystemUtil;

namespace {
bool
fileToProto(const std::string& path, google::protobuf::Message& out)
{
    int fd = open(path.c_str(), O_RDONLY);
    if (fd == -1)
        return false;
    else
        close(fd);
    FilesystemUtil::FileContents file(path);

        // more code down here, not useful to the problem.
Run Code Online (Sandbox Code Playgroud)

但是,当我编译时,我有:

build/Server/RaftLog.cc: In function ‘bool LogCabin::Server::RaftConsensusInternal::{anonymous}::fileToProto(const string&, google::protobuf::Message&)’:
build/Server/RaftLog.cc:43:17: error: ‘close’ was not declared in this scope
build/Server/RaftLog.cc: In function ‘void LogCabin::Server::RaftConsensusInternal::{anonymous}::protoToFile(google::protobuf::Message&, const string&)’: …
Run Code Online (Sandbox Code Playgroud)

c c++ unix

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

使用constexpr和返回模板函数的C++ 0x错误

我试图找到问题C++模板非类型参数类型推导问题的解决方案,它不涉及调用f的模板参数,但隐式选择模板参数的正确类型.

由于constexpr应该保证函数只包含编译时常量,并且在编译时进行评估(至少这是我认为的那样),我认为它可能是这个问题的解决方案.所以我想出了这个:

template <class T, T VALUE> void f() {}

//first i tried this:
template <class T> auto get_f(T t) -> decltype( &f<T,t> ) { return f<T,t>; }

//second try:
template <class T> constexpr void (&get_f( T t ))()  { return f<T,t>; }

int main()
{
    get_f(10)(); //gets correct f and calls it
}
Run Code Online (Sandbox Code Playgroud)

第一个版本生成以下错误:

error: use of parameter 't' outside function body
Run Code Online (Sandbox Code Playgroud)

这真的令人困惑,因为在尾部返回类型的decltype语句中使用参数应该没问题?

第二个版本生成以下错误:

error: invalid initialization of non-const reference of type 'void (&)()' 
       from …
Run Code Online (Sandbox Code Playgroud)

c++ templates constexpr c++11

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

为什么C#容器和GUI类对大小相关的成员使用int而不是uint?

我通常用C++编程,但是对于学校我必须用C#做一个项目.

所以我继续进行编码,就像我习惯于使用C++一样,但是当编译器抱怨代码如下时,我感到很惊讶:

        const uint size = 10;
        ArrayList myarray = new ArrayList(size); //Arg 1: cannot convert from 'uint' to 'int
Run Code Online (Sandbox Code Playgroud)

好吧,他们期望int作为参数类型,但为什么呢?我觉得uint作为参数类型会更舒服,因为uint在这种情况下更适合.

为什么他们在.NET库中几乎无处不在地使用int作为参数类型,即使在很多情况下负数也没有任何意义(因为没有容器或gui元素可以具有负大小).

如果他们使用int的原因是,他们并不期望普通用户关心签名,为什么他们没有为addint添加超载?

这只是MS不关心标志的正确性还是有负值值得一些意义/携带容器/ gui widget/...尺寸的一些信息(错误代码????)?

.net c#

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

数组初始化,引用前一个元素好吗?

const QPointF points[] =
{
    QPointF(r.left() - i, r.top() - i),
    QPointF(r.right() + i, r.top() - i),
    QPointF(r.right() + i, r.bottom() + i),
    QPointF(r.left() - i, r.bottom() + i),
    points[0] // is this line valid (according to the C++ standard)?
};
Run Code Online (Sandbox Code Playgroud)

虽然这与MS Visual Studio编译器编译,但我不确定这是否是根据C++标准的有效代码.

标准的报价将受到高度赞赏.

c++ arrays standards initialization

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

lambdas中for循环的问题

我目前正在重写我前一段时间写的一个小项目,并且正在用std::functionlambda 替换函数指针.

在这样做时,我偶然发现了lambda中的for循环问题.在lambda中使用for循环时,Visual Studio 2010(带SP1)会生成奇怪的错误,如果lambda是在文件范围定义的:

#include <iostream>

auto print_sum =
    []( int n )
    {
        int sum=0;
        // line below generates: 
        //   error C2143: syntax error : missing ')' before ';'
        for( int i=1; i<=n; ++i ) 
            sum += i;
        std::cout << sum << "\n";
    };

int main()
{
    print_sum(3);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

以下片段然后编译好:

#include <iostream>

int main()
{
    auto print_sum =
        []( int n )
        {
            int sum=0;
            for( int i=1; i<=n; ++i )
                sum += i; …
Run Code Online (Sandbox Code Playgroud)

c++ gcc visual-studio-2010 visual-studio c++11

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