小编mka*_*aes的帖子

在C++ 0X中评估auto类型

我正在使用C++ 0X标准中的自动功能,但我很困惑如何做出类型的决定.请考虑以下代码.

struct Base
{
    virtual void f()
    {
        std::cout << "Base::f" << std::endl;
    }
};

struct Derived : public Base
{
    virtual void f()
    {
        std::cout << "Derived::f" << std::endl;
    }
};

int main()
{
    Base* dp = new Derived;
    auto b1 = *dp;
    auto& b2 = *dp;
    std::cout << typeid(b1).name() << std::endl;
    std::cout << typeid(b2).name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

它将打印Base和Derived.
但是为什么被auto&评估为参考衍生而不是参考基础?
更糟糕的是将代码更改为:

struct Base{};
struct Derived : public Base{};

int main()
{
    Base* dp = new Derived; …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

13
推荐指数
2
解决办法
1199
查看次数

使用MPL创建所有模板排列

我有以下模板化的类结构

struct TraitA{};
struct TraitB{};

template<typename trait>
struct FunctionalityA{};

template<typename trait>
struct FunctionalityB{};

template<typename Func>
struct FuncUserA{};

template<typename Func>
struct FuncUserB{};

template<typename fuser>
struct Host{};
Run Code Online (Sandbox Code Playgroud)

Host类现在可以具有以下类型.

typedef Host<FuncUserA<FunctionalityA<TraitA> > > Host1_t;
typedef Host<FuncUserA<FunctionalityA<TraitB> > > Host2_t;
typedef Host<FuncUserA<FunctionalityB<TraitA> > > Host3_t;
typedef Host<FuncUserA<FunctionalityB<TraitB> > > Host4_t;
typedef Host<FuncUserB<FunctionalityA<TraitA> > > Host5_t;
typedef Host<FuncUserB<FunctionalityA<TraitB> > > Host6_t;
typedef Host<FuncUserB<FunctionalityB<TraitA> > > Host7_t;
typedef Host<FuncUserB<FunctionalityB<TraitB> > > Host8_t;
Run Code Online (Sandbox Code Playgroud)

有没有办法用boost :: mpl创建一个类型列表?目前我甚至没有想法从哪里开始.我的目标是拥有这样的功能:

template<class T>
T* getHost()
{
  typedef boost::mpl::find<HostVector, T>::type MplIter;
  return new …
Run Code Online (Sandbox Code Playgroud)

c++ boost metaprogramming boost-mpl

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

返回参数的Typename查找

最近我被学生问到编译问题.答案很简单,但现在我正在努力解决这个问题.一个简单的例子:

#include <iostream>
#include <vector>

struct MyStruct
{
    typedef std::vector<int> MyIntVector;

    MyIntVector CopyVector(MyIntVector const& vector);
};


MyStruct::MyIntVector MyStruct::CopyVector(MyIntVector const& vector)
^^^^^^^^
{
    MyIntVector vec;
    return vec;
}

int main(int /*argc*/, char** /*argv*/)
{
    MyStruct st;
}
Run Code Online (Sandbox Code Playgroud)

要成为有效的c ++代码,return参数必须是完全限定的.这么多的答案,让编译器/学生高兴.

但是为什么返回值要用类和函数的参数来限定?

我总是这样做,我知道它与ADL查找有关,但现在我被问到我在寻找更好的答案.
任何人都可以给我参考规格或提示我可以找到更多信息吗?

c++

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

联合交换结构

我想使用WinAPI中的InterlockedExchange来使用无锁的线程同步.
目前我有一个这样的课.

struct DataExchange
{
    volatile LONG m_value;
    void SetValue(LONG newVal)
    {
        InterlockedExchange(&m_value, newVal);
    }

    LONG GetValue()
    {
        LONG workVal=0;
        InterlockedExchange(&workVal, m_value);
        return workVal;
    }
};
Run Code Online (Sandbox Code Playgroud)

一个线程可以设置一个新值,另一个线程可以读取该值.
现在我想要做的是将LONG值更改为a struct.在WinAPI中有什么办法可以struct免费复制一个锁吗?

c++ windows winapi interlocked

6
推荐指数
1
解决办法
2248
查看次数

如何确定更大类型的decltype表达式

假设我有这样的函数:

static const boost::int32_t SOME_CONST_VALUE = 1073741823;
template<typename targetType, typename sourceType>
targetType Convert(sourceType source)
{
    typedef decltype(source * SOME_CONST_VALUE) MulType_t;
    //typedef boost::int64_t MulType_t;
    MulType_t val = (MulType_t)source * (MulType_t)SOME_CONST_VALUE;
    return val / (MulType_t)SOME_CONST_VALUE;
}
Run Code Online (Sandbox Code Playgroud)

当我这样调用这个函数时

boost::int32_t i = std::numeric_limits<boost::int32_t>::max();
boost::int32_t k = Convert<boost::int32_t>(i);
Run Code Online (Sandbox Code Playgroud)

k等于1,因为乘法过程中溢出.将一切都投射到boost::int64_t我想要的结果.但我不想将short或char转换为int64值.
因此,我可以使用decltype来获取下一个更大类型的表达式.

c++ c++11

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

错误:尝试插入两个地图时,'__ x <__y'中的'operator <'不匹配

在代码中有两个map.One商店对和其他商店,其中Values是类,带有5个变量,数据类型为字符串,int,string,int,int.但在插入第二个映射时,我收到错误g ++错误:不匹配尝试在地图中插入时,'__ x <__'中的'operator <'.(注意第一个地图中的键和值更改为值,第二个地图中的键更改)

如何解决它.

class Values
{
private:
    std::string C_addr;
    int C_port;
    std::string S_addr;
    int S_port;
    int C_ID;

public:
    Values(std::string,int,std::string,int,int);
    void printValues();
};


Values :: Values(std::string Caddr,int Cport,std::string Saddr,int Sport,int Cid)
{
    C_addr=Caddr;
    C_port=Cport;
    S_addr=Swaddr;
    S_port=Sport;
    C_ID=Cid;
}

void Values::printValues()
{
    cout << C_addr<<":" <<C_port<<":" << S_addr <<":" <<S_port << ":"<<C_ID  <<endl;
}


map<int, Values> items;
map<Values,int> itemscopy;

Values connection (inet_ntoa(Caddr.sin_addr),ntohs(Caddr.sin_port),inet_ntoa(Saddr.sin_addr),ntohs(Saddr.sin_port),CID);


for(unsigned int key=0;key<=30000;    )
{
    map<int,Values>::const_iterator itemsIterator=items.find(key);

    if(itemsIterator==items.end())
    {
        items.insert(pair<int, Values> (key, connection));
        {
            map<Values,int>::const_iterator itemsIterator1;
            if(itemsIterator1==itemscopy.end()) …
Run Code Online (Sandbox Code Playgroud)

c++ stl client-server map

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

将内存文件添加到 clang CompilerInstance

我正在尝试使用 clang 创建一个工具,并且想知道是否可以将包含文件从内存注入到预CompilerInstance处理器。
我的目标是向我的文件添加 a#include <my_globals.hpp>并动态地包含该文件以及适当的内容。所以我有ASTFrontendAction这样的:

class MyFrontendAction : public ASTFrontendAction
{
    virtual bool BeginInvocation(CompilerInstance &ci) override{
        auto buffer = llvm::MemoryBuffer::getMemBufferCopy(...);
        ci.createFileManager();
        ci.createSourceManager(ci.getFileManager());
        ci.createPreprocessor(clang::TU_Complete);
        auto& pp = ci.getPreprocessor();
        auto& preprocessorOpts = pp.getPreprocessorOpts();
        preprocessorOpts.clearRemappedFiles();
        preprocessorOpts.addRemappedFile("my_globals.hpp", buffer.release());
        // this seams not to work
        auto& hsi = pp.getHeaderSearchInfo();
        auto& headerSearchOptions = hsi.getHeaderSearchOpts();
        headerSearchOptions.Verbose = true; // this option is used during job

    }
    std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance& ci, StringRef file) override{/* do my parsing */}

};
Run Code Online (Sandbox Code Playgroud)

只要我不包含头文件,解析就可以工作。如果我这样做,我会得到一个my_globals.hpp file …

c++ clang libtooling

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

boost :: function的奇怪行为与函数指针

当我偶然发现以下问题时,我正在玩boost :: function.

typedef int(*IFnPtr2)(Interface*,int,bool&);
IFnPtr2 p = NULL;
boost::function<int(Interface*,int,bool&)> fn; // this is fine
boost::function<IFnPtr2> fn2; // this gives me a compile error
Run Code Online (Sandbox Code Playgroud)

我想知道为什么函数在与类型和typedef一起使用时表现不同,应该表示相同的类型.这对我来说不是问题,因为我只是不使用typedef,但我仍然很想知道为什么会有区别.
我使用的编译器是MSVC2010.

c++ boost

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

可变参数模板和参数推导

鉴于Anthony Williams的以下代码片段.一个非常基本的元组示例,这里的所有内容都按预期工作.

#include <iostream>

template<typename ... Types>
class simple_tuple;

template<>
class simple_tuple<>
{};

template<typename First,typename ... Rest>
class simple_tuple<First,Rest...>:
        private simple_tuple<Rest...>
{
        First member;
public:
        simple_tuple(First const& f,Rest const& ... rest):
          simple_tuple<Rest...>(rest...),
                  member(f)
          {}
          First const& head() const
          {
                  return member;
          }
          simple_tuple<Rest...> const& rest() const
          {
                  return *this;
          }
};

template<unsigned index,typename ... Types>
struct simple_tuple_entry;

template<typename First,typename ... Types>
struct simple_tuple_entry<0,First,Types...>
{
        typedef First const& type;
        static type value(simple_tuple<First,Types...> const& tuple)
        {
                return tuple.head();
        }
}; …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-templates c++11

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

纯c支持/ util库

我正在使用C/pragma/..中的库来搜索基本的编程任务.处理和创建列表和hasmaps和数组以及souch东西的东西.因此,我不必一次又一次地重新发明轮子并一次又一次地编写相同的结构.但必须是纯C库.

谢谢你的帮助.

c

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

在c ++中实现线程

我正在尝试用c ++中的线程维护两个函数(Visual Studio支持#include库),当我运行没有参数的函数时它运行正常,但是带参数会弹出错误.代码是:

void fun(char a[])
{}

int main()
{
    char arr[4];
    thread t1(fun);
    //(Error    1   error C2198: 'void (__cdecl *)(int [])' : too few arguments for call) 

    thread t2(fun(arr));     
    //Error 1   error C2664: std::thread::thread(std::thread &&) throw()' : 
    //cannot     convert parameter 1 from'void' to 'std::thread &&' 
    //Second Error is 2 IntelliSense: no instance of constructor
    // "std::thread::thread" matches the argument list argument types are: (void

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

帮我处理这件事.

c++ multithreading

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