小编Vin*_*ent的帖子

将const引用返回给C数组?

这是我的示例类:

template<typename T> class MyClassVector
{
    public:
        inline const std::vector<T>& data() const
        {
            return _data;
        }

    protected:
        std::vector<T> _data;
};

template<typename T, unsigned int SIZE> class MyClassArray
{
    public:
        inline const /* SOMETHING */ data() const
        {
            return _data; // OR SOMETHING ELSE
        }

    protected:
        T _data[SIZE];
};
Run Code Online (Sandbox Code Playgroud)

我的问题是:MyClassArray类的MyClassVector data()函数的等价物是什么,它返回对底层_data容器的常量引用?

非常感谢你 !

c++ arrays reference

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

constexpr vs编译时数学函数的模板?

我对C++ 2011的新关键字constexpr很困惑.我想知道在编写时间函数(特别是数学函数)时,在哪里使用constexpr以及在哪里使用模板元编程.例如,如果我们采用整数pow函数:

// 1 : 
template <int N> inline double tpow(double x)
{
    return x*tpow<N-1>(x);
}
template <> inline double tpow<0>(double x)
{
    return 1.0;
}

// 2 :
constexpr double cpow(double x, int N)
{
    return (N>0) ? (x*cpow(x, N-1)) : (1.0);
}

// 3 :
template <int N> constexpr double tcpow(double x)
{
    return x*tcpow<N-1>(x);
}
template <> constexpr double tcpow<0>(double x)
{
    return 1.0;
}
Run Code Online (Sandbox Code Playgroud)

第2和第3功能是否相同?什么是最好的解决方案?它是否产生相同的结果:

  • 如果x在编译时已知
  • 如果在编译时不知道x

何时使用constexpr以及何时使用模板元编程?

编辑1:修改代码以包括模板的专业化

c++ templates metaprogramming constexpr c++11

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

模板模板时强制特定的重载

请考虑以下代码:

#include <iostream>
#include <vector>
#include <type_traits>

// Version A
template<typename T>
void f(const T& x)
{
    std::cout<<"Version A"<<std::endl;
}

// Version B
template<template<typename> class T, typename T1>
void f(const T<T1>& x)
{
    std::cout<<"Version B"<<std::endl;
}

// Main
int main(int argc, char* argv[])
{
    f(double());
    f(std::vector<double>()); // <- How to force the use of version B ?
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

默认情况下,它将产生:

Version A
Version A
Run Code Online (Sandbox Code Playgroud)

如何强制使用Version B当传递的类型是具有良好形状的模板模板时(我可以添加新版本f,我可以添加std::enable_if或其他C++ 11类型特征语法,但如果可能的话我想避免添加帮助器上课)?

c++ templates metaprogramming c++11

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

检测两种类型是否是同一类模板的特化?

我想知道如何编写一个type_traits类来检测两个类型是否是同一个模板类的特化.最大的问题是它应该适用于混合类型/非类型模板类,如:

template <typename T, std::size_t N>
class MyClass {};
Run Code Online (Sandbox Code Playgroud)

有可能设计出这样的东西吗?

type-traits template-specialization template-meta-programming c++11

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

如果不可能,元函数计算x ^ n并返回整数限制而不会溢出?

请考虑以下代码:

template <std::intmax_t Base, std::intmax_t Exponent> 
struct integer_power_bounded
{
    static_assert(Exponent >= 0, 
                  "Error in 'integer_power_bounded': 'Exponent >= 0' is false");
    static constexpr std::intmax_t value = /* something */;
};

template <std::intmax_t Base> 
struct integer_power_bounded<Base, 0>
{
    static constexpr std::intmax_t value = 1;
};
Run Code Online (Sandbox Code Playgroud)

而不是/* something */,我想返回std::numeric_limits<std::intmax_t>::min()std::numeric_limits<std::intmax_t>::max()如果Base^Exponent不能用a代表std::intmax_t.困难的是避免在计算过程中出现溢出,因为它们在编译时会产生错误.

怎么做(没有提升)?

c++ metaprogramming integer-overflow template-meta-programming c++11

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

元函数将类型转换为整数,反之亦然

typeid允许分配一个唯一的std::type_index在运行时每个类型.我想做同样的事情,静态地使用两个元函数:

// Get a unique integral number associated with the provided type
template <class T>
struct encode_type
{
    using type = T;
    static constexpr std::size_t value = /* Metaprogramming magic */;
};

// Get the type uniquely associated with the provided value
template <std::size_t V>
struct decode_type
{
    static constexpr std::size_t value = V;
    using type = /* Metaprogramming magic */;
};
Run Code Online (Sandbox Code Playgroud)

有没有办法在C++ 11中做到这一点?

c++ templates types template-meta-programming c++11

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

博览会只在C++标准中?

这是什么的论述仅正是在C++标准意味着什么?这是否意味着标记的私人/受保护成员只需要按标准存在,或者它们只是实施的"建议",并不是必需的?

例子包括:

std::error_code::val_
std::wstring_convert::byte_err_string
std::array::elems
std::move_iterator::current
std::reverse_iterator::current
std::ostream_iterator::delim
// And a lot of others
Run Code Online (Sandbox Code Playgroud)

c++ standards c++-standard-library c++11 reverse-iterator

8
推荐指数
3
解决办法
458
查看次数

在constexpr函数中返回一个C字符串:为什么编译器没有警告?

请考虑以下代码:

constexpr auto f()
{
    auto str = "Hello World!";
    return str;
}

int main(int argc, char* argv[])
{
    static constexpr auto str = f();
    std::cout << str << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是正常的,我的编译器没有显示任何警告?它是否定义了行为?我能保证程序会显示"Hello World!"吗?我希望"Hello World!"不要超出功能范围......

c++ c-strings string-literals constexpr c++14

8
推荐指数
2
解决办法
642
查看次数

函数指针是C++中的函数对象吗?

C++标准将函数对象定义为:

函数对象类型是一种对象类型,可以是函数调用中postfix-expression的类型.(链接)

首先,我在想,函数对象是仿函数,但后来我意识到,对于一个函数指针ptr类型的P(不是功能,而是一个函数指针)std::is_object_v<P>true,可与被称为ptr(Args...)语法.

我认为函数指针被标准视为函数对象是对的吗?如果它们不是函数指针不满足定义的哪一部分?

c++ function function-object type-traits c++11

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

C ++ 17 / C ++ 2a中的编译时哈希类型

考虑以下代码:

#include <iostream>
#include <type_traits>

template <class T>
constexpr std::size_t type_hash(T) noexcept 
{
    // Compute a hash for the type
    // DO SOMETHING SMART HERE
}

int main(int argc, char* argv[])
{
    auto x = []{};
    auto y = []{};
    auto z = x;
    std::cout << std::is_same_v<decltype(x), decltype(y)> << std::endl; // 0
    std::cout << std::is_same_v<decltype(x), decltype(z)> << std::endl; // 1
    constexpr std::size_t xhash = type_hash(x);
    constexpr std::size_t yhash = type_hash(y);
    constexpr std::size_t zhash = type_hash(z);
    std::cout << (xhash == yhash) …
Run Code Online (Sandbox Code Playgroud)

c++ hash template-meta-programming c++17 c++20

8
推荐指数
2
解决办法
417
查看次数