相关疑难解决方法(0)

轻松测量经过的时间

我正在尝试使用time()来衡量我的程序的各个点.

我不明白为什么之前和之后的值是一样的?我知道这不是描述我的程序的最佳方式,我只想看看有多长时间.

printf("**MyProgram::before time= %ld\n", time(NULL));

doSomthing();
doSomthingLong();

printf("**MyProgram::after time= %ld\n", time(NULL));
Run Code Online (Sandbox Code Playgroud)

我试过了:

struct timeval diff, startTV, endTV;

gettimeofday(&startTV, NULL); 

doSomething();
doSomethingLong();

gettimeofday(&endTV, NULL); 

timersub(&endTV, &startTV, &diff);

printf("**time taken = %ld %ld\n", diff.tv_sec, diff.tv_usec);
Run Code Online (Sandbox Code Playgroud)

我如何阅读结果**time taken = 0 26339?这是否意味着26,339纳秒= 26.3毫秒?

那怎么说**time taken = 4 45025,这意味着4秒和25毫秒?

c c++ linux time measurement

272
推荐指数
12
解决办法
48万
查看次数

如何反转可变参数模板函数的参数顺序?

我有一个带有varargs模板参数模板函数,就像这样

template<typename Args...>
void ascendingPrint(Args... args) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

我想写

template<typename Args...>
void descendingPrint(Args... args) {
  /* implementation using ascendingPrint()? */
}
Run Code Online (Sandbox Code Playgroud)

如何在传递参数包之前反转参数包 的顺序args,即在伪代码中:

template<typename Args...>
void descendingPrint(Args... args) {
  ascendingPrint( reverse(args) );
}
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-functions variadic-templates c++11

32
推荐指数
5
解决办法
7028
查看次数

扩展参数包的默认函数参数可以"填写"吗?

以下代码无法编译:

#include <iostream>

template<typename F, typename ...Args>
static auto wrap(F func, Args&&... args)
{
    return func(std::forward<Args>(args)...);
}

void f1(int, char, double)
{
    std::cout << "do nada1\n"; 
}

void f2(int, char='a', double=0.)
{
    std::cout << "do nada2\n"; 
}

int main()
{
    wrap(f1, 1, 'a', 2.); 
    wrap(f2, 1, 'a'); 
}
Run Code Online (Sandbox Code Playgroud)
g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out

main.cpp: In instantiation of 'auto wrap(F, Args&& ...) [with F = void(*)(int, char, double); Args = {int, char}]':
main.cpp:22:20:   required from …
Run Code Online (Sandbox Code Playgroud)

c++ parameter-passing variadic-templates c++11 c++14

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

什么模式匹配(如果有的话)应用于C++可变参数模板函数调用?

我只是天真地写了这个:

#include <stdio.h>

template< class... Args >
auto format_for( Args... args, int last_arg )
    -> char const*
{
    // using Specifier = char const [3];
    // static Specifier const s[] = { {'%', 'f'+(0*args), ' '}..., {'%', 'f', 0} };
    return reinterpret_cast<char const*>( "" );
}

auto main() -> int
{
    printf( "'%s'\n", format_for( 5, 2, 1 ) );
}
Run Code Online (Sandbox Code Playgroud)

它崩溃了Visual C++ 2015更新1,一个ICE(内部编译器错误),并且g ++ 5.1.0维护该函数只接受一个参数,可能是因为忽略了Args无法匹配此模板参数:

C:\my\forums\so\081> cl printf.cpp /Feb
printf.cpp
printf.cpp(14): fatal error C1001: An internal …

c++ templates

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

类模板的可变构造函数模板的专业化

这是一个具有可变构造函数的类,它是用于复制和从临时移动的特殊化.

template<class Obj>
class wrapper {
protected:
   Obj _Data;
public:

   wrapper(const wrapper<Obj>& w): _Data(w._Data) {}

   wrapper(wrapper<Obj>&& w):
      _Data(std::forward<Obj>(w._Data)) {}

   template<class ...Args>
   wrapper(Args&&... args):
      _Data(std::forward<Args>(args)...) {}

   inline Obj& operator()() { return _Data; }

   virtual ~wrapper() {}
};
Run Code Online (Sandbox Code Playgroud)

当我使用这样的专业化之一

wrapper<int> w1(9);
wrapper<int> w2(w1);
Run Code Online (Sandbox Code Playgroud)

我收到了错误:w1的类型被推断为w1.

VS2012的输出:

error C2440: 'initializing' : cannot convert from 'win::util::wrapper<int>' to 'int'
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题呢?

c++ templates variadic-templates c++11

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

功能中的参数打包后的参数

我能够在SO 上找到一个问题,似乎在问与我相同或相似的问题,但是没有答案:-(

我想在参数包后放置一个非模板参数。我对可变参数模板/参数包的C ++标准规范不太熟悉,但是我的常识假设告诉我,传递给函数的最右边的参数将首先填充到放置参数中,然后将其余参数填充到参数包。但是,我无法在g ++或clang ++上使用我的测试代码。下面的示例代码。

#include <vector>
#include <iostream>

int Subscribe(int channel, int callback)
{
    return channel;
}

// This one works fine...
template<typename... T>
std::vector<int> SubscribeMultiple1(int callback, T&&... channels)
{
    return {
        Subscribe(std::forward<T>(channels), std::move(callback))...
    };
}

// This one does not work; all I did was move `int callback` after the parameter pack.
template<typename... T>
std::vector<int> SubscribeMultiple2(T&&... channels, int callback)
{
    return {
        Subscribe(std::forward<T>(channels), std::move(callback))...
    };
}

int main()
{
    auto subs = SubscribeMultiple2(1, 2, 3); …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-templates

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