小编Jus*_*ica的帖子

如何避免预编译头文件

我试图用C++编译一个简单的VS程序作为类的赋值.我们只包括<iostream>并且我一直收到此错误:

1> Assignment.cpp(15):致命错误C1010:查找预编译头时意外结束文件.你忘记添加'#include "StdAfx.h"'到你的来源了吗?

我的程序确实很小......

#include <iostream>
using namespace std;
int main()
{
    unsigned int day = 30;

    cout << "My Name is John Doe" << endl;
    cout << "My Major is CS" << endl;
    cout << "I was born on day " << day << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我刚刚安装了Visual Studio Express 2010.我真的很想开始一个空的项目,而不是安装所有这些预定义的文件,我认为它会使它变得更容易但我在创建项目时从未得到过这个选项.有人有什么建议吗?

c++ visual-studio-2010 visual-studio

65
推荐指数
3
解决办法
11万
查看次数

在新代码中,为什么要使用`int`而不是`int_fast16_t`或`int_fast32_t`作为计数变量?

如果您需要一个计数变量,肯定必须有一个整数必须支持的上限和下限.那么为什么不通过选择适当的(u)int_fastxx_t数据类型来指定这些限制呢?

c++ int c++11

9
推荐指数
2
解决办法
2277
查看次数

基于模板模板参数所采用的参数数量,部分特化模板的语法是什么?

请考虑以下代码:

template<typename>
struct One {};

template<typename, typename>
struct Two {};

template<template<typename...> class TTP, typename...>
struct SS;

#ifdef    TEST_TTP
    template<template<typename> class OneParam,
             typename... Ts>
    struct SS<OneParam, Ts...> {};

    template<template<typename, typename> class TwoParam,
             typename... Ts>
    struct SS<TwoParam, Ts...> {};
#else  // TEST_TTP
    template<template<typename> class OneParam,
             typename TParam>
    struct SS<OneParam, TParam> {};

    template<template<typename, typename> class TwoParam,
             typename TParam1,
             typename TParam2>
    struct SS<TwoParam, TParam1, TParam2> {};
#endif // TEST_TTP

int main() {
    SS<One, int>       ssoi;
    SS<Two, int, int> sstii;
}
Run Code Online (Sandbox Code Playgroud)

如果TEST_TTP未定义,此代码将在Clang,GCC和MSVC上正确编译.但是,如果它被 …

c++ g++ template-templates visual-c++ clang++

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

有没有一种非间接,非破解的方法来保证constexpr函数只能在编译时调用?

目前,我们有两个主要的编译时评估选项:模板元编程(通常使用模板结构和/或变量)和constexpr操作1

template<int l, int r> struct sum_ { enum { value = l + r }; }; // With struct.
template<int l, int r> const int sum = sum_<l, r>::value;       // With struct & var.
template<int l, int r> const int sub = l - r;                   // With var.
constexpr int mul(int l, int r) { return l * r; }               // With constexpr.
Run Code Online (Sandbox Code Playgroud)

其中,我们保证可以在编译时对所有这四个值进行评估。

template<int> struct CompileTimeEvaluable {};

CompileTimeEvaluable<sum_<2, 2>::value> template_struct;                 // Valid.
CompileTimeEvaluable<sum<2, 2>> …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr

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

可以重载的operator delete有默认参数吗?

我是运算符重载试验newdelete,并注意到MSVC和GCC出现在他们的实施而不同operator delete.请考虑以下代码:

#include <cstddef>

struct CL {
    // The bool does nothing, other than making these placement overloads.
    void* operator new(size_t s, bool b = true);
    void operator delete(void* o, bool b = true);
};
// Functions are simple wrappers for the normal operators.
void* CL::operator new(size_t s, bool b) { return ::operator new(s); }
void CL::operator delete(void* o, bool b) { return ::operator delete(o); }

auto aut = new (false) CL;
Run Code Online (Sandbox Code Playgroud)

此代码将使用GCC(使用Ideone和TutorialsPoint在线编译器进行测试)进行正确编译,但不能使用MSVC(使用MSVS 2010,MSVS …

c++ g++ operator-overloading visual-c++ default-arguments

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

如何处理使用不同类型索引(size_t、int、...)的不同库(例如 stl 和 eigen3)的混合

我有以下问题。我有一些使用 Eigen3 的代码。Eigen3 使用 int 或 long int 作为索引。在代码中的某些地方,我必须将特征数组中的值存储在std::vector.

这是一些例子:

std::vector myStdVector;
Eigen::VectorXd myEigen;
....

for(size_t i=0; i<myStdVector.size(); i++)
{
    myStdVector[i] = myEigen(i);
}
Run Code Online (Sandbox Code Playgroud)

这里我得到编译器警告:

警告:隐式转换会丢失整数精度:“const size_t”(又名“const unsigned long”)到“int”

所以我当然可以static_cast<int>(i)在发生这种情况的所有函数中添加 a ,但我想知道是否有更好的方法来处理这种情况。我想许多其他“库混合”也会发生这种情况。

c++ eigen3

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

委派和致电父类的构造函数:我两者都做吗?

我想知道关于派生类构造函数的委派。当您还必须调用父类的构造函数时,委托构造函数的正确方法是什么?我知道您不能在同一初始化列表中同时包含委托和成员初始化,但是我不知道调用父类的构造函数是否具有相同的限制。

// Option 1: Call parent class constructor, then delegate:

class Foo {
    public:
        Foo(int);
};

class Bar : public Foo {
    public:
        Bar(int, float) : Foo(int), Bar(int, float, 'c');
        Bar(int, float, char);
};

// Option 2: Delegate, then call parent class constructor:

class Foo {
public:
    Foo(int);
};

class Bar : public Foo {
    public:
        Bar(int, float) : Bar(int, float, 'c'), Foo(int);
        Bar(int, float, char);
};

// Option 3: Primary constructor calls parent class constructor:

class Foo {
    public: …
Run Code Online (Sandbox Code Playgroud)

c++ constructor c++11

4
推荐指数
2
解决办法
1641
查看次数

当尝试使用 decltype 生成指向重载函数的函数指针时,为什么无法指定适当的重载?

我正在尝试函子,无意中发现了一些decltype我觉得有趣的事情:据我所知,没有办法使用它来生成指向重载函数的函数指针,或为其提供解决所述重载所需的信息当尝试这样做时。

// Simple test code.
// Please ignore the possibility of attempting to dereference a nullptr function pointer,
//  proper checking was omitted for brevity.
template<typename... Ts> struct Functor;

template<typename F, typename R, typename... Ts>
struct Functor<F, R(*)(Ts...)> {
    Functor() : func(nullptr) { }
    Functor(F f) : func(f) { }

    R operator()(F f, Ts... ts) { return 2 * f(ts...); }
    R operator()(Ts... ts) { return 2 * func(ts...); }

    private:
        F func;
};

template<typename F>
decltype(auto) functorReturner(F …
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers decltype c++11

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