小编cpp*_*ner的帖子

C++ 14 TS功能和GCC 4.8

我想尝试一些新的功能,这将使它的C++ 2014 的修订std::make_uniquestd::filesystemfunctionnalities.我使用ubuntu 14.04和GCC/G ++ 4.8(安装了libstdc ++ - 4.8-dev)和标志-std=c++1y集.但是没有std::make_unique包含<tr1/memory>,也没有<experimental/...>标题.为了能够使用这些新功能,我需要做些什么?

谢谢 !

c++ c++14

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

具有静态大小数组作为参数的通用lambdas

以下是通用(多态)lambda合法C++ 14吗?

auto f = [](auto x[3]) {
    x[0];
    x[1];
    // etc.
};
Run Code Online (Sandbox Code Playgroud)

GCC和Clang 4接受代码,但Visual Studio 2017不接受.这合法吗?

error C3318: 'auto [3]': an array cannot have an element type that contains 'auto'
Run Code Online (Sandbox Code Playgroud)

c++ generic-lambda c++14

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

使用C++共享指针的别名构造函数和一个空的共享指针

std :: shared_ptr有一个别名构造函数,允许新创建的shared_ptr在指向某个其他对象时与现有共享指针共享状态.

我在考虑滥用这个构造函数来将指针放到shared_ptr中的一些全局对象:

int global = 0;

int main() 
{
    // because we point to global object we do not need to track its lifetime
    // so we use empty shared_ptr<void> as a provider of shared state
    std::shared_ptr<int> p(std::shared_ptr<void>(), &global);
    std::shared_ptr<int> pp = p; 
    return *pp;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:合法吗?该代码成功地适用于主要编译器.

请注意,我不会问这是否是一件好事.我知道有一种规范的方法可以使用no-op deleter将指针指向全局对象到shared_ptr中.如果它是合法的,它也有点令人不安,因为它可能有可解除引用的shared_ptr,弱指针总是过期的:

    std::shared_ptr<int> p(std::shared_ptr<void>(), &global);
    std::weak_ptr<int> w = p;
    if (p) // p is alive and well 
    {      // and w is not
        *w.lock(); // and here program crashes
    }
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++11

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

模板类型扣除失败?

请考虑以下示例.

#include <type_traits>
#include <iostream>
using namespace std;

template <typename T_> 
using Integral = typename std::enable_if<std::is_integral<T_>::value,T_>::type;
template <typename T_> 
using NotIntegral = typename std::enable_if<!std::is_integral<T_>::value, T_>::type;

template <typename T_>
void printIt(const Integral<T_> &value) { cout << "Integral == " << value << endl; }

template <typename T_>
void printIt(const NotIntegral<T_> &value) { cout << "Non Integral == " << value << endl; }

template <typename T_>
void foo(const T_ &value) { printIt<T_>(value); }

int main(int argc, char** argv)
{
    printIt<int>(66);   //Must …
Run Code Online (Sandbox Code Playgroud)

c++ templates type-traits c++11 type-deduction

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

为什么编译器接受具有long double literal的float的初始化?

我想知道为什么编译器不能容忍初始化浮点数一个const long double但允许使用long double literal初始化?我们不是在前者失去精确度吗?

float f {3.14L}; //compiles


const long double myConst {3.14};
float f{myConst}; // error: non-constant-expression cannot be narrowed from type 'long double' to 'float' in initializer list
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

在 C++20 中,如果宏被 #undef'd,然后再次 #define'd,那么它是否被认为是“活动的”?

考虑

#define VER 1
#undef VER
#define VER 2

#if defined(VER) // #1
#error VER is defined 
#endif
Run Code Online (Sandbox Code Playgroud)

[cpp.import]/6 :

如果宏指令在该位置之前的翻译单元中具有定义点,并且在该位置之前的翻译单元中没有未定义点,则该宏指令在源位置处处于活动状态

我的理解是在(the )之前VER有一个undefinition 点,因此它不被认为是active。也就是说,应该扩展到,并且该指令不应该生效。#1#undef VERdefined(VER)0#error

但在实践中,所有编译器都会产生一条错误消息VER is defined(这符合我的直觉,但不符合我对标准的理解)。

我的理解不正确吗?我错过了什么?

c++ language-lawyer c++20

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

向前将结构声明为类时出现Visual C ++ 2015链接器错误

我有以下代码(涉及多个文件)...

//--- SomeInterface.h
struct SomeInterface
{
  virtual void foo() = 0;
  virtual ~SomeInterface(){}
};

//--- SomeInterfaceUser.h
#include <memory> //shared_ptr

class SomeInterface;
//NOTE: struct SomeInterface... causes linker error to go away...

class SomeInterfaceUser
{
  public:
    explicit SomeInterfaceUser(std::shared_ptr<SomeInterface> s);
};

//SomeInterfaceUser.cpp
#include "SomeInterfaceUser.h"
#include "SomeInterface.h"
SomeInterfaceUser::SomeInterfaceUser(std::shared_ptr<SomeInterface> s)
{
}

//SomerInterfaceUserInstantiator.cpp
#include "SomeInterfaceUser.h"
#include "SomeInterfaceImpl.h"

struct SomeInterfaceImpl : SomeInterface
{
  virtual void foo(){}
};

void test()
{
  SomeInterfaceUser x{std::make_shared<SomeInterfaceImpl>()};
}
Run Code Online (Sandbox Code Playgroud)

使用Visual C ++编译器,我得到一个链接器错误(LNK2019)。使用GCC 4.8.4并非如此。将前向声明类SomeInterface更改为struct SomeInterface可使链接器错误消失。我一直以为应该可以互换使用类/结构?SomeInterfaceUser的接口不应取决于SomeInterface是定义为类还是struct,不是吗?

这是Visual C ++错误吗?我找不到任何与此有关的东西。我怀疑将struct用作模板参数这一事实与它有关。

感谢您的帮助。

c++ visual-c++

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

VS 2015中的std :: wcstok

我有这个使用过的用例代码,在使用VS 2015 C++编译器编译时产生警告.

#include <cwchar>
#include <iostream>

int main()
{
    wchar_t input[100] = L"A bird came down the walk";
    wchar_t* token = std::wcstok(input, L" ");
    while (token) {
        std::wcout << token << '\n';
        token = std::wcstok(nullptr, L" ");
    }
}
Run Code Online (Sandbox Code Playgroud)

这产生了以下警告.

warning C4996: 'wcstok': wcstok has been changed to conform with the ISO C standard, adding an extra context parameter. To use the legacy Microsoft wcstok, define _CRT_NON_CONFORMING_WCSTOK.
1>  c:\program files (x86)\windows kits\10\include\10.0.10240.0\ucrt\corecrt_wstring.h(254): note: see declaration of 'wcstok'

warning C4996: …
Run Code Online (Sandbox Code Playgroud)

c c++11 c11 visual-studio-2015

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

MSVC未正确评估定义(VARIABLE)?

运行以下代码

#include <iostream>

#define FOO
#define BAR defined(FOO)

int main() {
#if BAR
    std::cout << "BAR enabled!" << std::endl;
#else
    std::cout << "BAR disabled!" << std::endl;
#endif
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio中显示Bar disabled!,同时在gccclang显示中运行相同的代码Bar enabled!.

这是Microsoft编译器中的错误吗?根据标准,什么是正确的?

c++ gcc clang visual-c++ c-preprocessor

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

C++ 17支持Eclipse Neon

在这里读到C++ 17功能齐全,尽管规格还没有完全准备好.如何在我的代码中使用C++ 17功能,尤其是在Eclipse CDT(Neon)中?

具体来说,我想使用它filesystem来轻松迭代目录.

c++ eclipse-cdt c++17

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