小编Seb*_*n.M的帖子

C++ - 使用#include <ClassName>而不是#include <classname.h>有什么好处?

我知道标准库使用包含.h的旧C库,包括没有.h的最新库.但是对于自己的班级来说,更好的做法是什么?

在我工作的地方,我们总是有一个include文件夹,每个类有两个文件:classname.h和ClassName.ClassName包含classname.h,classname.h包含真正的类头.要包含您使用的课​​程

#include <ClassName>
Run Code Online (Sandbox Code Playgroud)

这就是Qt的方式,我很确定Qt是他们开始在我公司做的原因.但那有什么好处吗?

我认为缺点非常明显:

  • 为新类创建的另一个文件(我们使用bash脚本执行此操作,但仍然)
  • 每个类要管理一个文件
  • 通过IDE重命名文件/类时,通常需要手动调整ClassName文件(即使Qt Creator也不能这样做)
  • 除了std库和Qt之外,我从未见过这种风格

你用这种风格吗?如果是这样,为什么?它的论点是什么?是否有更多反对它的论据?

c++ qt include

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

测试两个模板是否相同,即使非类型参数包作为参数也是如此

所以我需要找出两个模板是否相同,即使参数不是,即.如果in T<A>U<B>T和U相同,即使A和B不相同.std::is_same不能使用,因为它只考虑完整类型.

我的第一个解决方案是:

template<typename T, typename U>
struct is_same_template : std::false_type {};

template<template<typename> typename T, typename A, typename B>
struct is_same_template<T<A>, T<B>> : std::true_type {};

template<template<typename> typename T, typename A, template<typename> typename U, typename B>
struct is_same_template<T<A>, U<B>> : std::false_type {};
Run Code Online (Sandbox Code Playgroud)

它有效,但仅适用于带有一个参数的模板,因此我将其扩展为:

template<typename T, typename U>
struct is_same_template : std::false_type {};

template<template<typename...> typename T, typename... A, typename... B>
struct is_same_template<T<A...>, T<B...>> : std::true_type {};

template<template<typename...> typename T, typename... A, template<typename...> typename U, typename... B>
struct …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

MSVC使用constexpr if在可变参数模板方法中吞下const的基本模板参数

我有一个问题,我几乎可以肯定是一个MSVC错误,但也许我错过了一些东西.

这是实际代码的简化版本:

template <typename... Args>
class InnerType {};

template <typename... Args>
class OuterType {
public:
    void foo1() {
        if constexpr (true) {
            bar(InnerType<Args...>());
        }
    }

    void foo2() {
        if (true) {
            bar(InnerType<Args...>());
        }
    }

    void bar(InnerType<Args...>) {}
};
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,唯一的区别foo1()foo2()是constexpr如果.当我尝试在MSVC中编译一些测试时会发生以下情况:

 OuterType<const bool> test1;
 test1.foo1(); // does not compile
 test1.foo2(); // compiles

 OuterType<bool> test2;
 test2.foo1(); // compiles
 test2.foo2(); // compiles
Run Code Online (Sandbox Code Playgroud)

我得到的错误test1.foo1()是:

error C2664: 'void OuterType<const bool>::bar(InnerType<const bool>)':
cannot convert argument 1 from 'InnerType<bool>' to 'InnerType<const bool>' …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++ variadic-templates if-constexpr

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

Qt Creator clang 代码模型“无法解析包含的文件”警告和派生模板类中的错误

我在 Qt Creator 中使用 clang 代码模型。它总是工作得很好,包括我重写从模板基类派生的类中的方法以及使用 C++17 功能时的情况。然后我切换到 Qt Creator 4.5,突然收到错误“只有虚拟成员函数可以标记为‘覆盖’”,即使代码没有改变。

现在我尝试返回 Qt Creator 4.4 并删除所有设置,但它仍然是相同的。

最重要的是,当我打开一个新文件时,我收到警告“代码模型无法解析包含的文件,这可能会导致代码完成和突出显示缓慢或不正确。type_traits:3083:7:错误:预期'(' 用于函数式转换或类型构造”,我收到错误“错误:命名空间 'std' 中没有名为 '可选' 的类型。

这仅影响代码模型,即。Qt Creator 在 IDE 中向我显示警告和错误。编译仍然可以正常工作。

这是我的代码模型配置:

-Weverything -Wno-c++98-compat -Wno-c++98-compat-迂腐 -Wno-unused-macros -Wno-newline-eof -Wno-exit-time-destructors -Wno-global-constructors -Wno -gnu-zero-variadic-macro-arguments -Wno-documentation -Wno-shadow -Wno-missing-prototypes -Wno-c++11-extensions -std=c++1z

我尝试了使用和不使用设置std,但这并没有改变任何东西。

这是一个导致“错误:使用未声明的标识符‘std’”、“错误:预期‘(’用于函数样式转换或类型构造”和“错误:使用未声明的标识符‘x’”的最小示例:

#pragma once

#include <optional>

class Test {
public:
    void bar() {
        std::optional<int> x;
    }
};
Run Code Online (Sandbox Code Playgroud)

有什么线索可能发生了变化或者我可能缺少哪些设置吗?

qt qt-creator c++17

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

如何在 Typescript 项目中将 git 子模块与 Typescript 代码一起使用

我正在开发一个 VueJS 项目,该项目根据环境变量使用不同的颜色和其他一些东西。这些值包含在我作为子模块添加的另一个存储库中。所以我的项目中的结构是:

  • 我的项目
    • 子模块
    • 节点模块
    • 民众
    • 源代码

为了完成这项工作,我将其包含在vue.config.js

module.exports = {
  chainWebpack: config => {
    config.resolve.alias
      .set('@theme', path.resolve('the-submodule/' + process.env.VUE_APP_THEME))
  },
...
Run Code Online (Sandbox Code Playgroud)

我这样使用它,例如plugins/vuetify.ts

import Vue from 'vue'
import Vuetify from 'vuetify/lib/framework'
import colors from '@theme/colors'

Vue.use(Vuetify)

export default new Vuetify({
  theme: {
    themes: {
      light: {
        ...colors
      }
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

或者在router/index.ts

import texts from '@theme/texts'

...

router.afterEach((to) => {
  Vue.nextTick(() => {
    document.title = to.meta.title ? `${texts.appName} | ${to.meta.title}` : texts.appName
  }) …
Run Code Online (Sandbox Code Playgroud)

typescript webpack vue.js

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

在lambda中捕获参数包时,一些int会丢失它们的值

所以我有一个可变参数模板类,它有一个方法,用于在lambda中捕获模板的参数.后来叫lambda.问题是,无论是在包的前面还是后面,它都会失去价值.

这是一个简化的例子:

#include <iostream>
#include <functional>

void doPrint() {}

template <typename Arg, typename... Args>
void doPrint(Arg arg, Args... args) {
    std::cout << arg << std::endl;
    doPrint(std::forward<Args>(args)...);
}

template <typename... Args>
void print(Args... args) {
    doPrint(std::forward<Args>(args)...);
}

class IntWrapper {
public:
    IntWrapper(int val) : val(val) {}

    int val;
};

std::ostream& operator<<(std::ostream& out, const IntWrapper& value) {
    out << value.val;
    return out;
}


template <typename... Args>
class TestClass {
public:
    void createWrapper(Args... args) {
        wrapper = [&]() -> void {
            print(std::forward<Args>(args)...);
        }; …
Run Code Online (Sandbox Code Playgroud)

c++ lambda variadic-templates

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