小编Syn*_*xis的帖子

嵌套模板类的特化语法

我试图找出嵌套模板类的显式特化的正确语法.以下代码将更好地说明:

struct Column_Major;
struct Row_Major;

template<size_t rows, size_t cols, typename T, typename Allocator>
class Matrix
{

    /* bunch of members */
    template <typename storage = Column_Major>
    class Iterator
    {
        /* bunch of members */
    };
};
Run Code Online (Sandbox Code Playgroud)

我想写一个明确的专业化template <> class Matrix<...>::Iterator<Row_Major,但语法正在逃避我.我怀疑如果没有包含类Matrix的显式特化,就不可能明确地专门化Iterator类.但如果有办法,我会很高兴.

我知道我可以使Iterator类成为一个单独的类,而不是Matrix类的成员,但是让嵌套的类允许我完全访问Matrix类的模板参数和数据库,这简化了事情.我知道如果需要,我可以解决这个问题,但我首先想调查并了解嵌套方法的可能性.

谢谢,Shmuel

c++ templates inner-classes template-specialization

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

自动添加 git 子模块 (.gitmodules)

我有一个包含几个子模块的仓库。我想添加一些其他的,对我来说最快的方法是使用.gitmodules(在我看来应该明确允许任何类型的子模块管理)。

但是,在编辑此文件并添加子模块时,在git submodule init添加任何内容之后(修改前已经存在的子模块除外)。

是否有任何解决方案可以在不经过的情况下添加子模块git submodule add(即,只需编辑.gitmodules文件然后git submodule update --init)?

也就是说,以下工作流应该自动添加子模块“ foo/bar ”:

Add the following to .gitmodules:
    [submodule "foo/bar"]
        path = foo/bar
        url = https://example.com/foo.git

Run the following command after saving:
    git submodule init
    git submodule update

Expected result:
    submodule 'foo/bar' automatically gets added
    it is also updated (the update command)
Run Code Online (Sandbox Code Playgroud)

git git-submodules

11
推荐指数
2
解决办法
3857
查看次数

static constexpr指向函数的指针,编译器之间的区别

在回答这个问题时,我尝试使用gcc(代码编译)和clang(拒绝代码)的以下代码:

typedef long (*func)(int);

long function(int) { return 42; }

struct Test
{
    static constexpr func f = &function;
};

template<func c>
struct Call
{
    static void f()
    {
        c(0);
    }
};

int main()
{
    Call<Test::f>::f();
}
Run Code Online (Sandbox Code Playgroud)

我不确定哪个编译器是正确的,虽然我认为constexpr初始化Test::f是好的.错误clang输出是:

error: non-type template argument for template parameter of pointer type 'func'
       (aka 'long (*)(int)') must have its address taken
Run Code Online (Sandbox Code Playgroud)
  1. 哪个编译器是对的?
  2. 如果clang是对的,为什么,这个错误究竟意味着什么?

编辑:对于"为什么",请参阅DyP的问题.

c++ static function-pointers constexpr c++11

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

默认模板参数的"重新定义"

使用Visual C++ 2010,我对以下代码有一个奇怪的编译警告:

#include <iostream>

class test
{
    public:
        template<class obj>
        class inner
        {
        private:
            // Line 11:
            template<int index, bool unused = true> struct AttributeName;

        private:
            template<bool b>
            struct AttributeName<0,b>
            {
                static inline const char* get()
                {
                    return "prop";
                }
            };

        public:
            typedef AttributeName<0> propname;
        };
        typedef inner<test> description;
};

int main()
{
    test t;
    std::cout << test::description::propname::get(); // Line 32
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

警告:

file.cpp(11) : warning C4348: 'test::inner<obj>::AttributeName' : redefinition of default parameter : parameter 2 (with …
Run Code Online (Sandbox Code Playgroud)

c++ templates visual-studio default-parameters

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

为什么atomic_flag默认构造函数未指定状态?

在使用时std::atomic_flag,必须小心,始终使用明确初始化它ATOMIC_FLAG_INIT,这很容易出错.但是有一个默认的构造函数......那么,是否有一个客观原因让默认构造函数将标志保留在非特定状态?

c++ constructor atomic

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

当用"class_name obj_name {func()}"替换"class_name obj_name = func()"时有意义吗?

在代码中,我看到以下构造:

const class_name obj_name{func()};
Run Code Online (Sandbox Code Playgroud)

func()返回一个名为的类的对象class_name.所以,我想知道为什么不使用以下结构:

const class_name obj_name = func();
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list assign

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

C++删除文件中的文本行

我的c ++程序有问题...我的整个程序是学生姓名,成绩和年龄的数据库,当用户想删除1名学生的数据时,我的功能有问题.这是代码:

void deletestudentdata()
{
    string name, grade, tname;
    int age, x=0; // x - "counter" to check if user entered wrong name

    system("cls");
    cout << "Enter name of the student you want to erase from database" << endl;
    cin >> tname;

    ifstream students("students.txt");
    ofstream temp("temp.txt"); // temp file for input of every student except the one user wants to delete

    while(students >> name >> grade >> age)
    {
        if(tname!=name){ // if there are students with different name, input their …
Run Code Online (Sandbox Code Playgroud)

c++ file

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

模板类中的编译时计数器

我有一个编译时计数器,我使用多年,受这些答案的启发.它适用于C++ 03/11,就我测试而言,在主要编译器上相对较好:

namespace meta
{
    template<unsigned int n> struct Count { char data[n]; };
    template<int n> struct ICount : public ICount<n-1> {};
    template<> struct ICount<0> {};

    #define MAX_COUNT 64
    #define MAKE_COUNTER( _tag_ ) \
        static ::meta::Count<1> _counter ## _tag_ (::meta::ICount<1>)
    #define GET_COUNT( _tag_ ) \
        (sizeof(_counter ## _tag_ (::meta::ICount<MAX_COUNT + 1>())) - 1)
    #define INC_COUNT( _tag_ ) \
        static ::meta::Count<GET_COUNT(_tag_) + 2> _counter ## _tag_ (::meta::ICount<2 + GET_COUNT(_tag_)>)
}
Run Code Online (Sandbox Code Playgroud)

以下测试编译并运行完美(预期输出为0 1 2 3):

struct …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates clang

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

如何将QLine添加到QGridLayout

我正在尝试开发一个Tic Tac Toe游戏,我将在QMainWindow中央小部件中放置一个QGridLayout.在那里,我打算添加其他小部件(板单元格)和分隔游戏单元的线条.

这可能吗?我找不到任何API在QGridLayout中插入QLine ..

如果无法做到这一点,我可以将子窗口小部件直接放在QMainWindow的中央窗口小部件上吗?如果是这样,怎么样?

c++ qt qt4

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

用C++复制到剪贴板?

我想在我的C++应用程序中将一些字符串复制到剪贴板.我怎样才能做到这一点?也可以复制格式吗?谢谢

c++ clipboard qt

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