小编Ale*_*ily的帖子

C++ 'using' 别名的多重定义

using在 C++ 中多次声明相同的别名是否合法?我有一个模板库,其中一个头文件用作“公共” - 它包含所有模板类声明和别名,然后是“实现”文件(不是字面意义上的,这些包含模板类的定义)。公共头文件在其末尾包含所有实现头文件。

不可能包含实现文件中的公共标头,因为这会导致循环依赖。但是,我想使用在实现文件内的头文件中声明的模板别名。

例子:

“公共”标头:

using true_type = logical_constant<true>;
using false_type = logical_constant<false>;
Run Code Online (Sandbox Code Playgroud)

“私人”实施文件:

using true_type = logical_constant<true>;
using false_type = logical_constant<false>;
Run Code Online (Sandbox Code Playgroud)

只要两者使用别名alias相同的东西,这合法吗?这个简单的例子在 MSVC 14 中对我有用,但是当我尝试以同样的方式使用更复杂的别名时,编译器会抱怨。

c++ templates using

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

C ++ SFINAE检测增量运算符

我正在尝试为类和原始类型检测预递增运算符的存在。我尝试了以下操作:

template <typename T>
struct has_pre_increment
{
    template <typename U, typename = decltype(++(std::declval<U>()))>
    static long test(const U&&);
    static char test(...);

    static constexpr bool value = sizeof(test(std::declval<T>())) == sizeof(long);
};

struct C
{
    int operator++();
};

struct D
{

};

int main()
{
    std::cout << std::boolalpha;
    std::cout << has_pre_increment<int>::value << std::endl;
    std::cout << has_pre_increment<C>::value << std::endl;
    std::cout << has_pre_increment<D>::value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

结果对于类是正确的,但是,因为int它无法检测到预递增运算符。你知道为什么吗?如何检测operator++任何类型?

c++ sfinae

6
推荐指数
0
解决办法
205
查看次数

std :: is_function的实现 - 为什么我的实现行为不同?

我有以下is_function的实现:

template <typename SomeType>
struct _is_function_helper : public _false_expression {};
template <typename ReturnType, typename ... ArgumentTypes>
struct _is_function_helper<ReturnType (ArgumentTypes ...)> : _true_expression {};
template <typename ReturnType, typename ... ArgumentTypes>
struct _is_function_helper<ReturnType (ArgumentTypes ..., ...)> : _true_expression {};

template <typename SomeType>
struct _is_function : public _boolean_expression<_is_function_helper<typename _remove_cv<typename _remove_reference<SomeType>::Type>::Type>::value> {};
Run Code Online (Sandbox Code Playgroud)

我删除引用,cv限定符,然后尝试继承与_is_function_helper相同的bool表达式.然后我尝试了以下测试:

void func(int,int) { };

struct A { void foo(int); };

....

auto r = func;
std::cout << std::boolalpha;
std::cout << std::is_function<decltype(func)>::value << " " << _is_function<decltype(func)>::value << std::endl;
std::cout << std::is_function<int(int)>::value …
Run Code Online (Sandbox Code Playgroud)

c++ templates type-traits

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

CRTP在基类的派生类中引用typedef

我有以下代码:

template <typename T>
class A
{
    typedef typename T::Type MyType;
};

template <typename T>
class B : public A<B<T>>
{
    typedef T Type;
};
Run Code Online (Sandbox Code Playgroud)

当我尝试实例化B时,我收到以下使用MSVS 2015的错误消息:

'Type': is not a member of 'B<int>'
Run Code Online (Sandbox Code Playgroud)

这段代码是有效的C++还是MSVS对吗?

c++ templates crtp

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

C++这不是成员函数指针吗?

根据以下测试:

std::cout << std::is_member_function_pointer<int A::*()>::value << std::endl;
Run Code Online (Sandbox Code Playgroud)

不是成员函数指针,而是常规函数,而这个:

std::cout << std::is_member_function_pointer<int (A::*)()>::value << std::endl;
Run Code Online (Sandbox Code Playgroud)

评估为真.我用gcc和msvc试过了.这两个声明之间有什么区别?这些结果是否正确?为什么括号A::*重要?

c++ type-traits

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

模板参数上的 C++ 函数模板重载

是否可以像这样重载函数模板(仅在使用 enable_if 的模板参数上):

template <class T, class = std::enable_if_t<std::is_arithmetic<T>::value>>
void fn(T t)
{

}
template <class T, class = std::enable_if_t<!std::is_arithmetic<T>::value>>
void fn(T t)
{

}
Run Code Online (Sandbox Code Playgroud)

如果条件enable_if不重叠?我的 MSVS 编译器抱怨说'void fn(T)' : function template has already been defined. 如果没有,有什么替代方法(理想情况下不要将enable_if其他任何地方放入模板参数中)?

c++ templates overloading

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

MSVS 2015表示错误C4146 - 应用于无符号类型的一元减运算符

有没有办法在Microsoft Visual Studio 2015中禁用错误​​C4146 - 快速版?我尝试将"将警告视为错误"选项设置为否,但MSVC仍将C4146视为错误.这有什么解决方案吗?

c++ compiler-errors visual-studio

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

C++放置新的vs复制赋值

在某处,我读到该向量内部使用了新的对象构造.当我试图实现类似的容器时,我通常最终得分配

_elements = new Type[capacity]

然后我添加这样的新对象

void append(const T& element) {
    // reallocation in case if capacity is equal to size
    _elements[size++] = element;
}
Run Code Online (Sandbox Code Playgroud)

这种结构通过复制和施工是否有任何表现或其他差异?

c++ placement-new

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

标准算法any_of(),all_of()和none_of()应用于空范围

我有一个与stl算法有关的问题.

http://www.cplusplus.com/reference/algorithm/我看到any_of(),all_of()并且none_of()在空范围应用时具有不同的返回值,但似乎只是它们的实现的结果.

您认为这些算法的正确返回值是什么?集合论是否回答了这些问题?

c++ algorithm

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

WinAPI SetWindowLongPtr - 更改windowProc

MSDN网站上,我发现以下内容:

使用GWLP_WNDPROC索引调用SetWindowLongPtr会创建用于创建窗口的窗口类的子类.应用程序可以子类化系统类,但不应该为另一个进程创建的窗口类创建子类.SetWindowLongPtr函数通过更改与特定窗口类关联的窗口过程来创建窗口子类,从而使系统调用新窗口过程而不是前一过程.应用程序必须通过调用CallWindowProc将未通过新窗口过程处理的任何消息传递给上一个窗口过程.这允许应用程序创建一系列窗口过程.

这是否意味着,每次调用SetWindowLongPtrGWLP_WNDPROC,都会创建新的子类,或者如果相同的过程多次作为参数传递,Windows是否足够智能创建子类一次?

c++ windows winapi

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

C++ std :: add_const无法正常工作?

我试过以下代码:

#include <iostream>
#include <type_traits>
int main() { 
    std::cout << std::is_const<std::add_const<int*&>::type>::value;
}
Run Code Online (Sandbox Code Playgroud)

并且输出为0.这是正确的行为吗?

c++ type-traits

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

模板参数中的静态断言

我想问一下是否可以在模板参数中插入静态断言。

假设我想创建类,StaticArray<T,N>并且我想让用户无法实例化大小等于 0 的类。有什么办法可以static_assert(N != 0, "error message")在我的类中插入类似的东西?

c++ static-assert c++11

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