小编chr*_*244的帖子

为什么我的模板化函数不会将'int'提升为'T',其中'T'='double'?

我有一个模板化的类typename T.它包含一个功能,

template <typename T, size_t a>
myClass<T,a> operator+(myClass<T,a> lhs, const T& rhs) {
    return lhs += rhs;
}

myClass<T,a> myClass<T,a>::operator+=(const T& rhs) {
    // Do addition, depends on 'a'.
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

例如,当我打电话给你的时候

myClass<double, 2> myObj_double_2(constructor args);
myObj_double_2 = myObj_double_2 + 5.2;
Run Code Online (Sandbox Code Playgroud)

我没有问题.

如果我打电话

myObj_double_2 = myObj_double_2 + 5;
Run Code Online (Sandbox Code Playgroud)

然后编译器给我一个像 - 的消息No match for 'operator+' (operand types are 'myClass<double, 2ul>' and 'int'). Candidates are ... note: deduced conflicting types for parameter 'const T' ('double' and 'int') …

c++ templates c++11 type-promotion c++14

21
推荐指数
3
解决办法
1708
查看次数

检查所有类型T的参数包

Jonathan Wakely 对问题的回答类型特征检查参数包中的所有类型是否是可复制构造的,这提供了一种简单的(ish)方法来检查参数包中扩展的所有变量是否属于同一类型 - 例如:

#include <type_traits>

namespace detail {
    enum class enabler {};
}

template <bool Condition>
using EnableIf =
    typename std::enable_if<Condition, detail::enabler>::type;

template<typename... Conds>
struct and_ : std::true_type {};

template<typename Cond, typename... Conds>
struct and_<Cond, Conds...>
        : std::conditional<Cond::value, and_<Conds...>,
        std::false_type>::type {};

template<typename... T>
using areInts = and_<std::is_same<T,int>...>;

template<typename... T>
using areMySpecificClass = and_<std::is_same<T,MySpecificClass>...>;
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚如何扩展它,例如areTypeT,编写一个模板.

我的第一次尝试偶然发现"参数包'T'必须位于模板参数列表的末尾".我最近的尝试编译,但如果我使用它然后我得到替换失败:

template<typename Target>
template<typename... T1>
using areT = and_<std::is_same<T1,Target>...>;
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

c++ variadic-templates c++11

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

在C++中创建通用对象的最佳方法?

主要使用C#进行编程后,我发现自己在C++方面处于亏损状态.然而,我需要创建一个C++应用程序,因为它只是一个更大的C++解决方案中的一个组件.

情况

  • 我有一个包含数据组件(对象)的struct(Parent).这可能是任何类型的数据组件 - 即自定义结构.
  • 只有Parent(创建Parent incl对象)的编译器和终端接收器需要知道对象内部数据组件的类型,因为数据只与它们相关.
  • 然而,Parent结构可能会传递几个方法,对象甚至其他进程.
  • 对象的数据选项是有限的,编译器和反编译器都知道数据类型的不同选项.因此他们能够以原始形式反编译对象
  • 然而,这些类型可以扩展(即,虽然限制它不一定是固定的),并且反编译器和编译器将来会更新

问题 - 我不想使用"模板"重新创建可能遇到此数据组件的每个方法和对象...也看到将来可能更改数据类型建议每个进程遇到长对象的模板开始并不理想.

我正在寻找类似于C#Object的东西 - 并编写了以下组件

  • 这是一个很好的解决方案,还是我将来遇到问题
  • 是否可以对此进行改进?我没想过(特别是不包括Object\impl.h?
  • 有完全不同/更好的解决方案吗?

代码

  • 我的Header文件中有以下内容

    struct Object
    {
        Object();
        // Return true if value has succesfully been set
        // Return false if there is no compatibility between Value and result.
        template <typename T>
        bool GetValue(T &result);
        template<typename T>
        bool SetValue(T value);
        virtual LPVOID GetObjectAddress() = 0;
        virtual const char* GetType() = 0;
    };
    
    template<typename T>
    struct ObjectType:public Object
    {
            ObjectType(T value);
            T Value; …
    Run Code Online (Sandbox Code Playgroud)

c++ generics struct

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

如何在Matlab条形图的X轴上插入无穷大符号?

如何将无穷大符号添加到Matlab条形图的X轴?

当然,可以插入无穷大符号,即插入'\infty'xlabel,如插入代码的最后一行所示.
但是,我想在x轴条上添加无穷大符号,而不是在x轴标签中.
我怎样才能做到这一点?为了详细说明,下面添加了以下脚本:

data=[1 2 3; 1 3 4; 3 1 2];
bar(data)
set(gca,'YLim',[0 3])
set(gca,'YTick',[0:0.5:3])
set(gca, 'YTickLabel',num2str(get(gca,'YTick')','%02.1f%%'))
set(gca,'Xtick',1:3,'XTickLabel',{'\infty' ; '20 dB'; '15 dB'})
xlabel('\infty dB') % x-axis label
Run Code Online (Sandbox Code Playgroud)

图像显示问题

matlab matlab-figure

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

Gcc中的概念-Lite(带有TS的链接)

最近这个问题的回答强调,gcc现在支持concepts-litesvn trunk的构建.

同样的问题链接到最新的 TS,N4377.编辑 - 答案有一个较新的TS.

一篇有用的论文标题为N3580 - 这是2013年,作者是Andrew Sutton,Bjarne Stroustrup和Gabriel Dos Reis.

可以找到N4377的调整列表,标记为N4434.这给出了N4377论文的3个建议更改,并列出了回复点Walter E. Brown.

这些论文/技术规格相似,但每种情况都有各种小的变化.

是否有一些简单的方法来发现gcc当前实现的内容?或者确实,计划实施?

作为一个附带问题:是否ConceptClang有一些类似命名的项目与c ++ 1z概念有关?一个邮件列表后,从2015年5月收集了一些想法,特别是:

除了她的C++ 0x工作之外,Larisse可能会为概念ts做一些工作.

没有进一步回复该线程.

c++ c++-concepts

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

将#includes 扩展为 C++ 的文本文件

是否可以扩展#includec++ 文件的行,可能使用C预处理器,这样我就可以读取扩展文件而#includes不是#included的文件?

具体来说,如果我有

文件A.cpp:

#include "fileB.H"

int main()
{
//do whatever
return(0);
}
Run Code Online (Sandbox Code Playgroud)

文件B.H:

#include "fileC.H"
//Some lines of code
Run Code Online (Sandbox Code Playgroud)

文件C.H

//Some other lines of code
Run Code Online (Sandbox Code Playgroud)

和输出:

//Some other lines of code
//Some lines of code

int main()
{
//do whatever
return(0);
}
Run Code Online (Sandbox Code Playgroud)

本质上,将包含的文件复制粘贴到一个大型文本/C++ 代码文件中,而不进行编译?

如果我使用相关运行 cpp,-I<Directory containing files to include>那么我会得到一个长文本文件,但不仅仅是代码,它给出了将传递给编译器的内容(废话!)

c++ c-preprocessor

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

Linux shell 中“find”命令的“-newer $file”选项如何工作?

我有以下linux命令。

find ${MOUNT_POINT} -type f -name "VM*" -newer $SENTFILE -print0 | xargs -0 -i cp {} ${TMP_DIR}
Run Code Online (Sandbox Code Playgroud)

我很难理解这个选项-newer $SENTFILE。有人能解释一下这个选项吗?

linux shell command

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

修改std :: vector函数(继承?)

我正在将一些Fortran90代码移植到C++中(因为我很愚蠢,为了保存"为什么?!").

Fortran允许在数组上指定范围,特别是从负值开始,例如

double precision :: NameOfArray(FirstSize, -3:3)
Run Code Online (Sandbox Code Playgroud)

我可以用C++编写这样的东西

std::array<std::array<double, 7>, FirstSize> NameOfArray;
Run Code Online (Sandbox Code Playgroud)

但现在我必须像索引一样NameOfArray[0:FirstSize-1][0:6].如果我想使用Fortran样式索引进行索引,我可以写一下

template <typename T, size_t N, int start>
class customArray
{
public:
    T& operator[](const int idx) { return data_[idx+start]; }
private:
    std::array<T,N> data_;
}
Run Code Online (Sandbox Code Playgroud)

然后

customArray<double, 7, -3> NameOfArray;
NameOfArray[-3] = 5.2;
NameOfArray[3] = 2.5;
NameOfArray[4] = 3.14; // This is out of bounds, 
                       // despite being a std::array of 7 elements
Run Code Online (Sandbox Code Playgroud)

所以 - 一般的想法是"不要在这里继承std ::'容器类'".我的理解是,这是因为,例如,std :: vector没有虚拟析构函数,因此不应该(不能?)以多态方式使用.

是否有一些其他的方式,我可以使用std::array,std::vector等等,并得到他们的职能"免费",同时覆盖特定的功能?

template<typename T, size_t …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance overloading operator-overloading c++11

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

编写符号Matlab函数,该函数根据其参数而变化

我可以编写这样的符号函数:

syms A B x X
y1(x) = A
y2(x) = B
Run Code Online (Sandbox Code Playgroud)

我该怎么写这样的函数y(x) = x<X ? y1(x) : y2(x)

matlab function

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

引用模板化类/对象的数据成员

如何在不同的模板化对象中放置对模板化对象的引用(模板化,虽然我不确定是否相关)类?

目前我有(简化):

template <typename T, size_t D>
class Field
{
public:
    template <size_t meshDim>
    Field(const Mesh<meshDim>& mesh, const std::string &fileName):
    fileName_(fileName),
    meshDim_(meshDim),
    mesh_(mesh) // <- this also doesn't compile, 
                // although I think it would if I had a data member named 
                // mesh_ that compiled
    {
        for (unsigned int d=0; d<D; d++) {
            field_[d].reserve(mesh.numCells());
        }
    }

private:
    std::vector<T> field_[D];
    std::string fileName_;
    const size_t meshDim_;
    template<size_t meshDim>
    const Mesh<meshDim> &mesh_; // <- This doesn't compile
};
Run Code Online (Sandbox Code Playgroud)

这会遇到编译时错误:data member 'mesh_' …

c++ templates c++11 c++14

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

找不到符号 - 类Iterator [Java]

我刚刚开始使用Java并收到错误:"找不到符号 - 类Iterator"

我的代码出了什么问题?

public class Notebook
{
    // Storage for an arbitrary number of notes.
    private ArrayList<String> notes;

    /**
     * Perform any initialization that is required for the
     * notebook.
     */
    public Notebook()
    {
        notes = new ArrayList<String>();
    }

    /**
     * Store a new note into the notebook.
     * @param note The note to be stored.
     */
    public void storeNote(String note)
    {
        notes.add(note);
    }

    /**
     * @return The number of notes currently in the notebook.
     */
    public int numberOfNotes() …
Run Code Online (Sandbox Code Playgroud)

java

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